Hi,
I am beginner to Android development and trying to develop speech
recognition software using speech recognition apis available in
Android. I am not able to get the recognized speech. Please can
anybody help me in this. Here is my code.
package com.Speech;
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.TextView;
public class SpeechRecognizer extends Activity {
private Button butSpeak;
private TextView txtRecSpeech;
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; //
Check this
private ListView mList; // Check this
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate our UI from its XML layout description.
setContentView(R.layout.main);
// Get display items for later interaction
butSpeak = (Button) findViewById(R.id.butSpeak);
mList = (ListView) findViewById(R.id.list);
// Check to see if a recognition activity is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),
0);
if (activities.size() != 0) {
butSpeak.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
startVoiceRecognitionActivity();
}
}
);
} else {
butSpeak.setEnabled(false);
butSpeak.setText("Recognizer not present");
}
}
/**
* Fire an intent to start the speech recognition activity.
*/
private void startVoiceRecognitionActivity() {
ArrayList<String> potentialResults = new ArrayList<String>();
potentialResults.add("hi");
potentialResults.add("hello");
potentialResults.add("bye");
potentialResults.add("welcome");
potentialResults.add("Good Morning");
potentialResults.add("android");
Intent intent = new
Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Tips:\n 1) Speak
clearly into the Mic.\n 2) Speak Loudly");
intent.putExtra(RecognizerIntent.EXTRA_RESULTS,
potentialResults);
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
/**
* Handle the results from the recognition activity.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE &&
resultCode == RESULT_OK) {
// Fill the list view with the strings the recognizer
thought it could have heard
ArrayList<String> matches =
data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
butSpeak.setText(matches.toString());
mList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
matches));
}
super.onActivityResult(requestCode, resultCode, data);
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en