/Android Speech Recognition

Android Speech Recognition

First you need to create android project then put this XML file in the path:

res/layout

in this file we will create a button to receive our request to speak and a list view which will list the suggested results for our speech.

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

    <Button
        android:id="@+id/b_speak"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Speak" />

    <ListView
        android:id="@+id/lv_voicereturn"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>

Then, create a class named VoiceReco in your src path on this activity

#see comments in source code blow

First : we have to set up our layout and the list view , set click listener to our button which invoke on click method.

Second: in onclick method we make an intent which will fire speech recognition by passing an action called ACTION_RECOGNIZE_SPEECH

Third : passing some data to our intent such as

  1. Inform the recognizer which speech model to prefer when performing.
  2. Optional text prompt to show the user when asking them to speak.
  3. startActivityForResult

Finally we have to override onActivityResult method which will receive the result of the speech recognizer

  • check if the request sent successfully .

  • receive results in an array list of strings.

  • then show the results in a list view by using Array adapter.

package com.app;

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.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.view.View.OnClickListener;



public class VoiceReco extends Activity implements OnClickListener{
	ListView lv; 
	int check=121; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// first
		setContentView(R.layout.voice);
		Button b=(Button)findViewById(R.id.b_speak);
		lv=(ListView)findViewById(R.id.lv_voicereturn);
		b.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// second
		Intent in= new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		//third
		in.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 								RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
		in.putExtra(RecognizerIntent.EXTRA_PROMPT, "speak");
		startActivityForResult(in, check);
		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		super.onActivityResult(requestCode, resultCode, data);
		//finally
		if(requestCode==check && resultCode == RESULT_OK){
			
			ArrayList<String>results=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,results));
			
		}
	}
	

	
}

Note : this example may not work on your android emulator but it will work fine on your android phone.

 

Computer science student at faculty of computers and information systems-Mansoura University,Egypt,member@catreloaded Team. python developer Interested in IT field generally and specially open source technologies.