Android

Make your next Android app a good listener

This tutorial demonstrates how simple it is to use the Android Speech to Text API.

Last week I wrote a post on how easy it is to add text-to-speech capability to your Android applications.  It's easy because Google's speech API does all the heavy lifting for us. But the API doesn't stop at speaking text aloud -- it also allows you to speak to the device and then translates those voice commands into text an application can use.

This tutorial demonstrates how to add voice recognition to your next Android app. Feel free to follow along or download the entire project and import it directly into Eclipse.

1.  Create a new Android project in Eclipse.  Target Android 2.2 or higher.

2.  In the /res/layout directory, modify the activity_main.xml file to include a text view and an image button.

activity_main.xml
<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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="132dp"
        android:text="..." ></TextView>

    <ImageButton
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text1"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:layout_marginTop="37dp"
        android:src="@android:drawable/ic_btn_speak_now" ></ImageButton>

</RelativeLayout>

3.  In the /src folder, open MainActivity.java. The on create override should look familiar; it simply wires up the button.

MainActivity.java
package com.authorwjf.talk2me;

import java.util.ArrayList;

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

public class MainActivity extends Activity implements OnClickListener {

	protected static final int REQUEST_OK = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findViewById(R.id.button1).setOnClickListener(this);
	}


}

4.  The on click handler is responsible for firing off the voice intent.
@Override
public void onClick(View v) {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
         i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
        	 try {
             startActivityForResult(i, REQUEST_OK);
         } catch (Exception e) {
        	 	Toast.makeText(this, "Error initializing speech to text engine.", Toast.LENGTH_LONG).show();
         }
}

5.  When the intent calls back, we display the transcribed text.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==REQUEST_OK  && resultCode==RESULT_OK) {
        		ArrayList<String> thingsYouSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        		((TextView)findViewById(R.id.text1)).setText(thingsYouSaid.get(0));
        }
    }

Load the resulting APK to your Android device via a USB cable and try it out.

SpeechtoText_FigA_071513.png

SpeechtoText_FigB_071513.png

If your device isn't running JellyBean, you will need Internet connectivity for the voice input to work.  If your device is running JellyBean, you can go to Settings | Language & Input | Voice Search and download the offline speech recognition pack.

About

William J Francis began programming computers at age eleven. Specializing in embedded and mobile platforms, he has more than 20 years of professional software engineering under his belt, including a four year stint in the US Army's Military Intellige...

1 comments
ccollaor
ccollaor

Dear William:

Great tutorial, thank. Please, Could you tell me, how can I implement  your tutorial to play music from sd card or music that I've loaded in my table?, Nexus 7 for example?

Best regards,

Carlos