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



96 Comments

  • Sid 10 September, 2013, 20:49

    This part is not right:
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, “en-US”);

    Should be:
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, “en-US”);

    thanks!

  • Eman Hassan 11 September, 2013, 18:52

    I am trying to change the language of the recognition from English to Arabic and I simply did this

    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, “ar-QA”); // Qatari Arabic

    But I get the text in English which is fine with me but interpreted wrong

    for example, I say “Yameen = right” >> I get the text as “Yeah mean” :)

    any suggestions on how I might be able to change the language of the recognition and the text?!

    another question, why do I have the button to activate the recognition, can’t I simply keep it listening the min I run the app?

    Thank you so much in advance for helping me understand :)

    • SARA 16 October, 2013, 18:44

      use this

      Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
      
      intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
      intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ar-SA");
      
  • Ajay Prasad 21 September, 2013, 9:14

    Hi Viral Patel thanks for such a nice tutorial this program working perfect but i have some question regarding this post, this program recognize only alphabets not numeric or number if i say 100 it will print hundred, actually i want a result if i say 100 then it will show numeric value not a alphabets, can you help me out on this ?????

  • pratap 30 September, 2013, 15:45

    Hello viral patel,

    Thanks for sample application.

    It is working good. but in some devices it is working fine with out internet connection.
    but in some devices , it needs internet connection.

    Is there any other option to make speech to text in offline mode with out internet.

    Thanks,
    PRatap

  • abhijit 8 October, 2013, 10:27

    Is it possible to run the recognizer intent in the background and it is possible to convert a audio file to text by using this API in android.

  • Yash Gupta 9 October, 2013, 12:09

    Hey i want to know can i manually give input to the api . Like a recording to the api to convert it to text ??

  • Krunal 16 October, 2013, 1:20

    Hello,
    Great and easy example, can you please give me some suggestions my problem is i want to use the same functionality in a simple java class (non-activity class). how can i achieve this .?

  • purushoth 20 October, 2013, 23:08

    hi viral,i’m purushoth,i seen your project it’s fine k k ,i tried in my system this was not run properly if i click the button it’s record my speech it shows only the button(its first screen shot only displayed} help me…

  • Rakesh 21 October, 2013, 10:16

    What is the language tag for hindi-India?

  • shabaz 22 November, 2013, 21:45

    i download the source code and running it on sdk vertiual andriod device by it giving me error that (opp’s your device do not support speech to text ) what i have to do now

    • hector 5 December, 2013, 3:48

      maybe you have to cut and paste!

    • Thuannguyen 20 March, 2014, 15:33

      You must run the Android Project in real device (smartphone). It isn’t run in vitural device because vitural device not supported

  • hector 5 December, 2013, 3:47

    das ist bundaba!, excellent, muy bacano—- felicitaciones

    • Julian 23 June, 2014, 21:48

      Es heißt wunderbar. Aber egal…

  • Anil rai 10 December, 2013, 4:42

    sir, this speech to text demo is not working in w-ifi.plz send me the code so it work in w-ifi.
    I am still waiting for your answer.

  • HAR 21 December, 2013, 14:45

    vertiual andriod device display an error …opp’s your device do not support speech to text …..any one can help me

    • Luckas 2 January, 2014, 2:59

      Speech recognition is not supported by it, you have to test on some Android device with this functionality and this API.

  • bery 9 January, 2014, 9:53

    please,please who can answer my question:when i change the language of the recognition from English to Arabic i use(ar_SA) it is done but without any open,vibrio,Fracture and serenity on the arabic letters
    ,so what should i do to get the open,vibrio,Fracture and serenity on the arabic letters?
    Thank you so much in advance for helping me understand

  • kalthum 10 January, 2014, 17:48

    please help me soon:i want that when i read quran karim the program write it with full arabic language i mean with open,vibrio,Fracture , serenity and others on the arabic letters ,i change
    en-US to ar_SA its done but but without any open,vibrio,Fracture , serenity and others on the arabic letters ,so what should i do,please?or whats the type of english language that i can use to write every letter when i said in arabic language then it writes in english letter for example when i say “بِسْمِ الله” it will write “bismilah”.
    please,please answer me

  • mark 31 January, 2014, 22:51

    In my device, this application is not running without an Internet connection. What’s wrong?

  • sangeeta 1 February, 2014, 19:41

    hey can anybody mail me code for intenet class

  • krishna kanth 24 February, 2014, 1:25

    r.id cannot be resolved error plz help me guys..!!!

    • vinay 7 March, 2014, 7:02

      Clean your project. If you still see the error then make sure you are importing the correct R class.
      android.R is the OS provided class while yourpackagename.R will be your class generated on build.

  • getinet 7 March, 2014, 16:07

    I want to change the language of the recognition from English to Amharic language.

    any suggestions on how I might be able to change the language of the recognition and the text?!

  • stephen s d 14 March, 2014, 11:57

    hello sir, i am doing a project on voice recognization i,e text to voice and voice to text, sir i got text to voice and i wrote the code for voice to text, but in emulator its coming that this device doesn’t support this application, so what to do sir please suggest me and also which supporting files and are needed to run this application, reply me as fast as possible sir please.

  • Pothiraj 20 March, 2014, 18:49

    Nice coding.. 100 % working

  • Es 28 March, 2014, 0:44

    i want the opposite tts arabic – text arabic to speech – have you solution

  • Luis 30 March, 2014, 21:48

    Hi im having some problems , when im creating the layout this problem appear:
    @string/speak: No source found that matches the given name.

  • krishna 22 April, 2014, 15:24

    this source code is very understandable.thank you

  • Aravind 24 April, 2014, 19:15

    hi,

    Can anybody confirm me that this code works in offline mode…. for me its working fine in online but when iam trying in offline mode.. it does’t work….. when i went through the code there is no option indicating that internet is required?… so i think this should work in offline….pls suggest ….
    any references which work in offline….

    Thanks
    Aravind

  • Izy 29 April, 2014, 12:03

    Hello sir
    thanks for the tutorial…I encounter a problem where when I speak to the device then it load of something..after that the textview area doesn’t show up..any idea why??
    im using samsung galaxy mini
    looking forward for your respond..thank you :)

  • shruthi 14 May, 2014, 11:54

    THank u

  • sumit yadav 26 May, 2014, 13:19

    Thanks Viral. Its Nice Tutorial. It works perfectly on real android device. Good Luck.

  • Anthony 8 June, 2014, 18:13

    Will Android Speech take pre-recorded audio?

  • praveen 26 June, 2014, 7:17

    i want speech to text conversion in java please help me

  • praveen 29 June, 2014, 7:54

    how do i change my pacakage name…..net.viralpatel.android.speechtotextdemo

  • madhu 5 July, 2014, 14:59

    thanq …u helped me

  • Ravi Teja M 2 August, 2014, 13:06

    While i m trying this app in emulator it is showing the toast message “your device doesn’t support speech to text”. May i know the reason?

  • Dhruvika 19 August, 2014, 19:47

    hello sir
    i am Dhruvika.i am mca final year student.in 5 th sem dessertation subject .my dessertation defination is speech to text in gujarti for android mobile.i have no idea how to add gujrati voice typing languge in mobile.

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]

Current ye@r *