Android: Speech To Text using API

Android has a very cool feature that still many developers dont know. Apps like Any.DO uses speech to text conversion feature quite creatively. In today’s world of Siri, voice commands are of utmost importance. Android natively provides feature of Speech to Text so why not to use it in our app!

I will show you how to use Android’s Speech to Text API in an application.

Let’s make our demo application.

Demo App

The App will be very simple. It will have a button with Mic symbol. On click of which we trigger Android’s Speech to Text Intent which shows a dialog to take speech input. The speech input is then converted into text. The text is then displayed in a text view.

Step 1: Create Basic Android Project in Eclipse

Create a Hello World Android project in Eclipse. Go to New > Project > Android Project. Give the project name as SpeechToTextDemo and select Android Runtime 2.1 or sdk 7. I have given package name net.viralpatel.android.speechtotextdemo.

Once you are done with above steps, you will have a basic hello world Android App.

Step 2: Change the Layout

For our demo, we need simple layout. Just one Image Button to trigger Speech to Text API and one TextView to display result text that is converted from speech.

Open layout/main.xml in your android project and replace its content with following:

File: res/layout/main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_toLeftOf="@+id/textView1"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/btnSpeak"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:contentDescription="@string/speak"
        android:src="@android:drawable/ic_btn_speak_now" />

    <TextView
        android:id="@+id/txtText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

The UI is very simply. One LinearLayout to organize the button and text view. Note the id for button: btnSpeak and text view: txtText which we will use in our Java code.

Step 3: Android Java Code to trigger Speech to Text API

Open SpeechToTextDemoActivity class and replace the code with following.

File: SpeechToTextDemoActivity.java

package net.viralpatel.android.speechtotextdemo;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	protected static final int RESULT_SPEECH = 1;

	private ImageButton btnSpeak;
	private TextView txtText;

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

		txtText = (TextView) findViewById(R.id.txtText);

		btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

		btnSpeak.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(
						RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

				intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

				try {
					startActivityForResult(intent, RESULT_SPEECH);
					txtText.setText("");
				} catch (ActivityNotFoundException a) {
					Toast t = Toast.makeText(getApplicationContext(),
							"Opps! Your device doesn't support Speech to Text",
							Toast.LENGTH_SHORT);
					t.show();
				}
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

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

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

				ArrayList<String> text = data
						.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

				txtText.setText(text.get(0));
			}
			break;
		}

		}
	}
}

The heart of Speech to text Android API is package android.speech and specifically class android.speech.RecognizerIntent. Basically we trigger an Intent (android.speech.RecognizerIntent) which shows dialog box to recognize speech input. This Activity then converts the speech into text and send backs the result to our calling Activity. When we invoke android.speech.RecognizerIntent intent, we must use startActivityForResult() as we must listen back for result text.

Note how in above code we crate intent android.speech.RecognizerIntent and trigger it. Also we add one extra parameter using .putExtra() method. When invoking RecognizerIntent, we must provide extra RecognizerIntent.EXTRA_LANGUAGE_MODE. Here we are setting its value to en-US.

Since we triggered the RecognizerIntent via startActivityForResult(), we override method onActivityResult(int requestCode, int resultCode, Intent data) to handle the result data. The
RecognizerIntent will convert the speech input to text and send back the result as ArraList with key RecognizerIntent.EXTRA_RESULTS. Generally this list should be ordered in descending order of speech recognizer confidence. Only present when RESULT_OK is returned in an activity result. We just set the text that we got in result in text view txtText using txtText.setText().

One thing worth noting here is how to handle devices/android version that doesn’t support speech to text API. In such case, exception ActivityNotFoundException will be thrown when we try to start activity. In above example, we have catched this exception and displayed a message “Opps! Your device doesn’t support Speech to Text” using Toast.

Screen shots of Android App

And that’s all! Just execute the app in Android emulator or real device and see following output.

android-speech-to-text-api-demo

android-speech-to-text-activity

android-speech-to-text-conversion

android-speech-text

Download Source Code

Android_SpeechToTextDemo.zip (350 KB)

References

RecognizerIntent.html#ACTION_RECOGNIZE_SPEECH Documentation



38 Comments

  • Shubhajit paul 31 August, 2012, 14:37

    Excellent

  • Priya 5 September, 2012, 15:37

    Sir,This is actually good but i’m not getting text. after your third screen i’m getting “connection problem speak again”. I’m using Android 2.3

    • Sathishkumar 26 January, 2013, 12:37

      @Priya: For solving Connection problem You have to connect internet…

      I have also got same problem.. After connect internet It works for me.. Try this…

  • bryan 17 September, 2012, 7:53

    Is it possible to simultaneously capture the audio to a file, so I would have the original audio and transcript? Or would I have to record to a file and transcribe the file somehow? Thanks!

  • Brinda 18 October, 2012, 20:29

    It gives me “Your device doesn’t support this program”
    Please help me what to do.I have downloaded the zip of this and I am not understanding what is the problem.I also created AVD with Audio recorder support but still it gives me the same problem.
    Please help me

  • Suyog 21 October, 2012, 0:10

    I have the same issue as Brinda. When I click on the button in the emulator I get the message: “Oops! Your device doesn’t support speech to text”

    I have already added audio recorder and audio playback to the emulator. What is the problem?

  • rajesh 22 October, 2012, 21:30

    @brinda , suyog , the speech does not work on emulator try on real device

  • Gost 23 October, 2012, 2:08

    hi ! great job man. i have a question. can i have the possibility to change the “en-US” to another one.

  • Damodharan 29 October, 2012, 14:38

    Really superb dude, It is working fine in my android HTC wildfire . Thanks

    • ana 16 February, 2013, 15:38

      Its not working on emulator and giving error “Oops your device does not supplort speech to text”. How you run this code in htc wild Fire? plz guide me…

  • Carroll Perkins 1 November, 2012, 23:59

    I want to automatically text my voice replies while conversing on the phone. Is this possible and are there any apps?

  • Lokesh 2 November, 2012, 15:34

    shown Error as connection Problem..

  • Rafael 7 November, 2012, 8:45

    Unlike text to speech, speech to text must have an internet connection to provide the words spoken out. Text to speech do not need internet connection it uses the built-in android text to speech recognition (Pico TTS Engine) which you can find in settings under “Voice input and output settings”

  • narendra 24 November, 2012, 10:41

    Its working fine in my device by removing the android:showAsAction=”never” from the menu.activity_main. Can you please tell me what is the meaning of this and why we do required it in the application.

  • Yash 1 January, 2013, 15:54

    there is many errors…
    “unable to resolve target android-12″

  • Yash 1 January, 2013, 16:03

    r cannot be resolved to a variable in 12th line and 18th line but there are nothing….

  • Jitendra Patel 19 January, 2013, 16:54

    wow! it’s easy and amazing ….thanks sir….
    “txtText.setText(text.get(1));” insted of “txtText.setText(text.get(0)); ” for long speech.

  • kuldeep 22 January, 2013, 12:21

    i just wanted to know tht how could i listen to emails using text to speech engine provided by google i hv created an app which reads sms bt i also want it to read emails.send me appropriate code if possible.

  • Rakesh 22 January, 2013, 19:48

    Hello sir,
    the exception is occuring on mobile as well as emulator. ” “Opps! Your device doesn’t support Speech to Text”"
    What should i do ?

  • Steve 7 February, 2013, 15:35

    Great article. Thanks. Worked it in to my app to save to database!

  • Hisam 10 February, 2013, 8:10

    is it possible to make it in an offline mode? tnx.

  • Nikhil 12 February, 2013, 16:16

    Hi,
    I want to change the dialog box of google mike icon when speech to text is on . I want to customize it the way I want . Is it possible bcoz there are apps on google play with speech to text in which the have used different mike icon e.g: of app is text by voice from google play.

  • Eshrath 13 February, 2013, 11:11

    displayed a message “Opps! Your device doesn’t support Speech to Text” . sir can you help me how to handle device in android..

  • Surendra 19 February, 2013, 12:19

    Great Article. Thanks. I just want to know one thing can we record our voice with this???

  • shanawaz 20 February, 2013, 12:22

    from where to download eclipse software…some1 give me the link for that

  • Nagarjan 20 February, 2013, 21:54

    Hai friends ! what is the procedure to change the lanugage????????

  • Steve 25 February, 2013, 19:08

    Hi, nice quickstart tutorial but I noticed one thing …

    you said:
    we must provide extra RecognizerIntent.EXTRA_LANGUAGE_MODE. Here we are setting its value to en-US.

    That’s not true, the possible values are
    LANGUAGE_MODEL_FREE_FORM and LANGUAGE_MODEL_WEB_SEARCH
    (http://developer.android.com/reference/android/speech/RecognizerIntent.html#EXTRA_LANGUAGE_MODEL)

    To specify a language (different from what is given by Locale.getDefault() ) for the recognition you have to add another extra:
    EXTRA_LANGUAGE with the code of the language you want to recognize
    eg. intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, new Locale(“de”, “DE”).toString());
    (http://developer.android.com/reference/android/speech/RecognizerIntent.html#EXTRA_LANGUAGE)

    best, Steve

  • rishi 3 March, 2013, 1:00

    I am getting an error while using it “can not be resolved …” at btn_speak pls help

  • pranay 9 March, 2013, 17:45

    thanx buddy

  • Kulbhushan 14 March, 2013, 17:21

    In this app when i stop speaking then it stops recognizing the voice is it any way to continue to recognize voice for some time and then stop the activity

    • Guy 20 March, 2013, 12:54

      also wait for answer, same problem as this one

  • raj 14 April, 2013, 16:06

    how to create one key on the keyboard for speach to text in android

  • Alaa 17 April, 2013, 15:24

    pleas how can I make speech to text program work continuously without click the button every time I want to speak ??

  • Sohel 1 May, 2013, 23:34

    Sir, i have found an error here:
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
    }

    here, menu cannot be resolved or it is not find view.
    How can i do now

  • pranjal 6 May, 2013, 19:39

    if at run time it shows connection problem mean u have to add audio to your manifest file.

  • jayesh 13 May, 2013, 12:16

    dude i m using android 4.2.2 n it is saying that this device dosen’t support speech to text conversion……

  • ajeeb 16 May, 2013, 17:29

    hi..i got 2 errors here..

    txtText = (TextView) findViewById(R.id.txtText);

    btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

    why this 2 things got error..somebody plss help me…

Leave a Reply

Your email address will not be published. Required fields are marked *

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]