Voice Recognizer

This example shows how you can use android’s voice recognizer with Intent.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it VoiceRecognizer.

2.) Write following into main.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Recognizer + TTS"
/>
<Button
android:id="@+id/startrecognizer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Recognizer"
/>
<Spinner
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

 

 

3.) Run for output.

Steps:

1.) Create a project named VoiceRecognizer and set the information as stated in the image.

Build Target: Android 4.4
Application Name: VoiceRecognizer
Package Name: com.example. VoiceRecognizer
Activity Name: VoiceRecognizerActivity

2.) Open VoiceRecognizerActivity.java file and write following code there:

 

package com.example.voicerecognizer;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class VoiceRecognizerActivity extends Activity implements OnInitListener{

TextToSpeech tts;

Button startRecognizer;
Spinner spinnerResult;

private static final int RQS_RECOGNITION = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startRecognizer = (Button)findViewById(R.id.startrecognizer);
startRecognizer.setEnabled(false);

spinnerResult = (Spinner)findViewById(R.id.result);

startRecognizer.setOnClickListener(startRecognizerOnClickListener);

tts = new TextToSpeech(this, this);

}

private Button.OnClickListener startRecognizerOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"Speech to Recognize");
startActivityForResult(intent, RQS_RECOGNITION);
}
};

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if((requestCode == RQS_RECOGNITION) & (resultCode == RESULT_OK)){

ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, result);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerResult.setAdapter(adapter);

spinnerResult.setOnItemSelectedListener(spinnerResultOnItemSelectedListener);

}
}
private Spinner.OnItemSelectedListener spinnerResultOnItemSelectedListener
= new Spinner.OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String selectedResult = parent.getItemAtPosition(position).toString();
Toast.makeText(VoiceRecognizerActivity.this, selectedResult, Toast.LENGTH_SHORT).show();
tts.speak(selectedResult, TextToSpeech.QUEUE_ADD, null);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}};
@Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
startRecognizer.setEnabled(true);
}
}

 

 

3.) Compile and build the project.
4.) Application will crash if run on simulator. Try to run on actual device which supports voice recognition to check correct output.

Output