Android Voice Recognition Example


Google provides an option to search the web using voice command . Let us create a similar Voice Recognition Application( Speech to text Application) in android. In this example we will have a button to initiate speech recognition, by tapping on it we call the voice/speech recognition API where the voice is recognized and converted into text .Finally we will display the results that was recognized in a ListView.

Voice recognition featured in android is achieved using the RecognizerIntent.Use the Recognizer class in an intent to call the voice API.Here we check for the recognizer in the device if available the speech to text happen else we show a toast displaying 'Recognizer Not Found'

Note: This application will not work in emulator since it does not have the recognizer, so test it in a device and it also requires an internet connection.

So Lets Start:

1.Create an android application with any package name and project name.Name the Activity as VoiceActivity and the layout file as voice_demo.
2.Let us design the UI for the activity with a TextView,Button and ListView as follows

voice_demo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="Click the button below and start speaking"
        android:textSize="18dp" />

    <Button
        android:id="@+id/speakButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/voic_img" />

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

</LinearLayout>


3.In the Activity file we call the RecognizerIntent.ACTION_RECOGNIZE_SPEECH. Starts an activity that will prompt the user for speech and send it through a speech recognizer. The results will be returned via activity result.

VoiceActivity:

package com.example.voice;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class VoiceActivity extends Activity {

 private static final int REQUEST_CODE = 1234;
 private ListView resultList;
 Button speakButton;

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

  speakButton = (Button) findViewById(R.id.speakButton);

  resultList = (ListView) findViewById(R.id.list);

  // Disable button if no recognition service is present
  PackageManager pm = getPackageManager();
  List<resolveinfo> activities = pm.queryIntentActivities(new Intent(
    RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
  if (activities.size() == 0) {
   speakButton.setEnabled(false);
   Toast.makeText(getApplicationContext(), "Recognizer Not Found",
     1000).show();
  }
  speakButton.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    startVoiceRecognitionActivity();
   }
  });
 }

 private void startVoiceRecognitionActivity() {
  Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
  intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
    "AndroidBite Voice Recognition...");
  startActivityForResult(intent, REQUEST_CODE);
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == REQUEST_CODE &amp;&amp; resultCode == RESULT_OK) {
   ArrayList<string> matches = data
     .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
   resultList.setAdapter(new ArrayAdapter<string>(this,
     android.R.layout.simple_list_item_1, matches));
  }
  super.onActivityResult(requestCode, resultCode, data);
 }
}




4.Alter the manifest file by enabling internet permission
<uses-permission android:name="android.permission.INTERNET" />
5.Run the application by right clicking the project Run As->Android Application.You would see and output similar to the one below

Output: