Android Speech to Text Tutorial

person folder_openAndroidaccess_time June 15, 2015

In this tutorial we are going to show how to use Android’s Speech To Text API. Android currently supports offline mode also. The only thing is that you have to downlad offline language packages.

To learn how to enable offline mode speech to text, please follow the tutorial How to enable offline Speech To Text in Android

See following Steps :

  1. Start RecognizerIntent intent with the RecognizerIntent.ACTION_RECOGNIZE_SPEECH as action.
  2. Handle the text data returned by Speech to Text API

1. Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.stackandroid.speechtotext"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

2. Activity layout file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg_color"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/txt_output"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:textColor="@color/white"
        android:textSize="26dp"
        android:textStyle="normal" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="60dp"
        android:gravity="center"
        android:orientation="vertical" >
        <ImageButton
            android:id="@+id/btn_mic"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="@null"
            android:scaleType="centerCrop"
            android:src="@drawable/microphone" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Speech to text using Google API"
            android:textColor="@color/white"
            android:textSize="15dp"
            android:textStyle="normal" />
    </LinearLayout>
</RelativeLayout>

3. Main Activity code

Start the speech recognizer intent using startActivityForResult() with bundle extras.

Required extras:

Optional extras:

Result extras (returned in the result, not to be specified in the request):

Note: The extra EXTRA_LANGUAGE_MODEL is mandatory. Also handle ActivityNotFoundException there may be no applications installed in the device to support this speech recognition action.

package com.stackandroid.speechtotext;
import java.util.ArrayList;
import java.util.Locale;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
	private final int SPEECH_RECOGNITION_CODE = 1;
	private TextView txtOutput;
	private ImageButton btnMicrophone;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		txtOutput = (TextView) findViewById(R.id.txt_output);
		btnMicrophone = (ImageButton) findViewById(R.id.btn_mic);
		btnMicrophone.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				startSpeechToText();
			}
		});
	}
	/**
	 * Start speech to text intent. This opens up Google Speech Recognition API dialog box to listen the speech input.
	 * */
	 private void startSpeechToText() {
		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
				RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
		intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
				"Speak something...");
		try {
			startActivityForResult(intent, SPEECH_RECOGNITION_CODE);
		} catch (ActivityNotFoundException a) {
			Toast.makeText(getApplicationContext(),
					"Sorry! Speech recognition is not supported in this device.",
					Toast.LENGTH_SHORT).show();
		}
	 }
	 /**
	  * Callback for speech recognition activity
	  * */
	 @Override
	 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		 super.onActivityResult(requestCode, resultCode, data);
		 switch (requestCode) {
		 case SPEECH_RECOGNITION_CODE: {
			 if (resultCode == RESULT_OK && null != data) {
				 ArrayList<String> result = data
						 .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
				 String text = result.get(0);
				 txtOutput.setText(text);
			 }
			 break;
		 }
		 }
	 }
}

4. Demonstration
Speech to text demonstration using RecognizerIntent .

Speech to text demo
Speech to text demo

Offline mode is not enabled

Offline mode not enabled
Offline mode not enabled

Comments

  1. the offline mode is not working i have enabled the offline mode still it just retry again and again pls help

  2. Can I use Hindi recognization to English text

  3. What is the purpose of result.get(0)? why zero?

    1. Vinothkumar Arputharaj

      The index of ArrayList starts from 0. So, result.get(0) will return the first element (ie. 0th index) of the ArrayList

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">