SpeechRecognizer does not hear after the first result

I use SpeechRecognizer and RecognizerIntent in Android to implement speech recognition. My goal is to restart listening to the speech after my speech recognizer displays the results on the screen. For this purpose I use the following code.

The problem is that the first time it runs fine and displays the results, but after it starts listening for the second time (called by the onResults method), it does not hear what they are saying for some reason. Then it throws an error ERROR_SPEECH_TIMEOUT, which means there is no voice input. On Logcat, I see how it enters ReadyForSpeech (), but for some reason it will not hear what I say.

Does anyone know why this might happen? Does listening continue after it returns the result? Is it right to call startListening again?

public class VR extends Activity implements RecognitionListener { private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; private TextView vrtext; private SpeechRecognizer speech = null; private Intent intent; private String TAG = "VR"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.vr); vrtext = (TextView) findViewById(R.id.vrtext); } @Override public void onResume() { listen(); super.onResume(); } private void listen() { speech = SpeechRecognizer.createSpeechRecognizer(this); speech.setRecognitionListener(this); intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en"); intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName()); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3); speech.startListening(intent); } @Override protected void onPause() { super.onPause(); // TODO Auto-generated method stub if(speech != null) { speech.destroy(); Log.i(TAG,"destroy"); } } public void onBeginningOfSpeech() { // TODO Auto-generated method stub Log.i(TAG, "onbeginningofspeech"); } public void onBufferReceived(byte[] arg0) { // TODO Auto-generated method stub //Log.i(TAG, "onbufferreceived"); } public void onEndOfSpeech() { // TODO Auto-generated method stub Log.i(TAG, "onendofspeech"); } public void onError(int arg0) { // TODO Auto-generated method stub Log.i(TAG, "error code: " + arg0); } public void onEvent(int arg0, Bundle arg1) { // TODO Auto-generated method stub Log.i(TAG, "onevent"); } public void onPartialResults(Bundle arg0) { // TODO Auto-generated method stub Log.i(TAG, "onpartialresults"); } public void onReadyForSpeech(Bundle arg0) { // TODO Auto-generated method stub Log.i(TAG, "onreadyforspeech"); } public void onResults(Bundle arg0) { // TODO Auto-generated method stub Log.i(TAG, "onresults"); ArrayList<String> matches = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); String s = ""; for (String result:matches) s += result + "\n"; vrtext.setText(s); speech.startListening(intent); } public void onRmsChanged(float arg0) { // TODO Auto-generated method stub //Log.i(TAG, "onrmschanged"); } } 
+3
android speech-recognition voice-recognition speech
Jun 25 '12 at 19:29
source share
2 answers

"Does the listening continue after it returns the result?" No

"Is it right to call startListening again?" Yes.

In addition, if you want to constantly maintain recognition, you must call startListening again if some errors occur:

 @Override public void onError(int errorCode) { if ((errorCode == SpeechRecognizer.ERROR_NO_MATCH) || (errorCode == SpeechRecognizer.ERROR_SPEECH_TIMEOUT)) { Log.d(TAG, "didn't recognize anything"); // keep going recognizeSpeechDirectly(); } else { Log.d(TAG, "FAILED " + SpeechRecognitionUtil .diagnoseErrorCode(errorCode)); } } 

Check my code for using SpeechRecognizer to find a specific spoken word here .

+2
Jun 27 2018-12-12T00:
source share

Make sure you use a single SpeechRecognizer object within the action. A quick and dirty way to make it static.

 private static SpeechRecognizer speech = null; 

Modify your listen() method to check for null on the speech object.

 private void listen() { if (speech == null) { speech = SpeechRecognizer.createSpeechRecognizer(this); speech.setRecognitionListener(this); } intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en"); intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName()); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3); speech.startListening(intent); } 

Call the listening method in onResults() and in onError() .

 public void onResults(Bundle arg0) { // TODO Auto-generated method stub Log.i(TAG, "onresults"); ArrayList<String> matches = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); String s = ""; for (String result:matches) s += result + "\n"; vrtext.setText(s); //speech.startListening(intent); listen(); } public void onError(int arg0) { // TODO Auto-generated method stub Log.i(TAG, "error code: " + arg0); listen(); } 

And finally, don't forget to do the necessary cleanup in onDestroy() .

 @Override public void onDestroy() { super.onDestroy(); speech.destroy(); } 
+1
Aug 08 '14 at
source share



All Articles