Main navigation

Android Speech to Text Tutorial

adsense

Download full code here:

android speech to text tutorial

What is Android Voice Recognition App

We hope you would have heard about Android Voice Recognition App. It is a simple Speech to text Converter done using Android voice recognition APIs. Below is a small sample code of Android Speech to text tutorial. We have try to describe each and every step but still if you have any doubts then please comment or Download Code from top or bottom of this tutorial.

Note: This App doesn’t support Emulator. So please run it on real Android Device.

Creating a New Project – Android Speech to Text Tutorial

  1. Open Android Studio and create a new project Android Speech to Text Tutorial and company domain application.example.com (We have used our own company domain i.e androidtutorialpoint.com. Similarly you can use yours).
  2. Click Next and choose Min SDK, we have kept the default value. Again Click Next and Choose Blank Activity .
  3. Choose the Activity as MainActivity and click next.
  4. Leave all other things as default and Click Finish.

A new project will be created and gradle will resolve all the dependencies.

Layout of Android Speech to Text Tutorial

Here we will add a button that will ask for users to speak. Also there will be TextView to write on screen.

activity_main.xml

<RelativeLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/voiceInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:textSize="26sp"
        android:textStyle="normal" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:clickable="true"
            android:gravity="center"
            android:padding="16dp"
            android:textColor="@color/colorAccent"
            android:background="@color/colorPrimary"
            android:id="@+id/btnSpeak"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Tap to Open Mic"
            android:textSize="22sp"
            android:textStyle="normal" />
    </LinearLayout>

</RelativeLayout>

Speech to Text Android Code

This is the main part of voice recognition app where we will user RecognizerIntent to convert speech into text. Add following code in MainActivity.java:

MainActivity.java

package com.androidtutorialpoint.androidspeechtotexttutorial;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    private TextView voiceInput;
    private TextView speakButton;
    private final int REQ_CODE_SPEECH_INPUT = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        voiceInput = (TextView) findViewById(R.id.voiceInput);
        speakButton = (TextView) findViewById(R.id.btnSpeak);

        speakButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                askSpeechInput();
            }
        });

    }

    // Showing google speech input dialog

    private void askSpeechInput() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                "Hi speak something");
        try {
            startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
        } catch (ActivityNotFoundException a) {

        }
    }

    // Receiving speech input

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case REQ_CODE_SPEECH_INPUT: {
                if (resultCode == RESULT_OK && null != data) {

                    ArrayList<String> result = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    voiceInput.setText(result.get(0));
                }
                break;
            }

        }
    }
}

In the above code, we are triggering an Intent named RecognizerIntent which asks for speech input and then sends it through speech recognizer. It does it through ACTION_RECOGNIZE_SPEECH. The results will be returned via activity results in onActivityResult(int, int, Intent). If request code is REQ_CODE_SPEECH_INPUT then corresponding text is written in output screen.

So our code of Android Speech to Text Tutorial is complete. Run this code. Following screen will appear:

Android Speech to Text Tutorial

After you tap button at bottom, following screen will appear that will ask you to speak something.

Android Speech to Text Tutorial

Now if you speak anything, it will be shown on screen. For Example: I said “Android Tutorial Point is Best” So following screen appeared:

Android Speech to Text Tutorial




What’s Next ??

You can see our other tutorials on material design like Android ScrollView, ViewPager and Expandable ListView Tutorials etc.

We hope you liked our Android Speech to Text Tutorial, please leave comments in the comment section below in the case of any doubts. Let us know how do you liked this Android Speech to Text Tutorial.

To download the full code of Android Speech to Text Tutorial, Click on the Download Now link below.

Android Speech to Text Tutorial


Reader Interactions

Comments

  1. Thank you very much!

    Your tutorial works exactly as you have said.

    I have searched and searched for an updated tutorial, and this is the first truly easy example to follow.

    Thanks again!

  2. I want to create a login page with the help of speak to text. for example when I say action user name AAA okay then it catch only AAA and write it in user name field similarly password when I say action password 123 okay then it goes to password field and write 123 in the password field.
    When I say action he start listening voice and when I say okay then he stop listening
    if any one have idea about it please share here or send me on [email protected]

  3. I want to create a login page with the help of speak to text. for example when I say action user name AAA okay then it catch only AAA and write it in user name field similarly password when I say action password 123 okay then it goes to password field and write 123 in the password field.
    When I say action he start listening voice and when I say okay then he stop listening

  4. Hello every one.
    i have a problem in my speech to text app.
    i want to keep mic on.
    for example when i start my app and click on mic then the mic is still on and when i stop my app then the mic should off automatically please guide me

Leave a Reply

Advertisment ad adsense adlogger