6

I am improving an Android application which uses RecognitionListener class to listen user voice, here i get below results:

1-) If user click on microphone icon and said something everything is fine 2-) If user click on microphone icon and click again on microphone icon or did not say anything , i get onerror and error type is : ERROR_RECOGNIZER_BUSY

 @Override
 public void onError(int error) {
 if ((error == SpeechRecognizer.ERROR_NO_MATCH)
  || (error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT)){

  }  
  else if(ERROR_RECOGNIZER_BUSY){
  }

}

Here is my code for starting listening:

 public void recognizeSpeechDirectly()
     {


        recognizer = SpeechRecognizer.createSpeechRecognizer(this.context);
        recognizer.setRecognitionListener(this);
        recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "org.twodee.andytest");
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
        recognizer.startListening(recognizerIntent);

     }

I want to restart listening when ERROR_RECOGNIZER_BUSY appears,

Another guy told about this error on stackoverflow but it is not clear for me and cannot implement it.

How to handle ERROR_RECOGNIZER_BUSY

Thanks in advance

Community
  • 1
  • 1
odincer
  • 329
  • 6
  • 16

2 Answers2

1

You have ERROR_RECOGNIZER_BUSY because you call startListening twice when a user clicks the button and clicks again. Change your code as follow:

// class member
private boolean mIsListening;  
@Override
protected void onCreate(Bundle savedInstanceState)
{
    .........
    recognizer = SpeechRecognizer.createSpeechRecognizer(this.context);
    recognizer.setRecognitionListener(this);
    recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "org.twodee.andytest");
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
}

And when the icon is clicked

if (!mIslistening)
{
    mIsListening = true;        
    recognizer.startListening(recognizerIntent);
}  

@Override
public void onError(int error) {
 if ((error == SpeechRecognizer.ERROR_NO_MATCH)
  || (error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT)){

  }  
  else if(ERROR_RECOGNIZER_BUSY){

  }
  recognizer.startListening(recognizerIntent);
}  

@Override
    public void onPartialResults(Bundle partialResults)
    {
        mIsListening = false;
         ..........
    }  

@Override
    public void onResults(Bundle results)
    {
        mIsListening = false;
          ..........
    }
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • 1
    Thanks for reply Hoan but when I cancelled recongnizer and restart listening in **if(ERROR_RECOGNIZER_BUSY)** , instead of restarting listening the same error repeat in infinite loop. – odincer Mar 17 '13 at 11:39
  • Hi Hoan, i certainly applied what you said but it doesn't work for me, do you any working sample ?My machine is samsung S3 with android 4.1 by the way. – odincer Mar 17 '13 at 19:34
  • What is wrong now? do you still have error ERROR_RECOGNIZER_BUSY? – Hoan Nguyen Mar 17 '13 at 19:38
  • `if(ERROR_RECOGNIZER_BUSY) recognizer.cancel();` then `if (!mIslistening) mIsListening = true; recognizer.startListening(recognizerIntent);` properly works , but it does not restart listening again when i click on micro icon second time. Actually below link says that we can not cancel operation when **onerrors** comes on [link]http://stackoverflow.com/questions/5849063/does-recognitionlistener-onerror-automatically-speechrecognizer-cancel – odincer Mar 17 '13 at 19:43
  • Did you put mIsListening = false; everywhere I had in the above code? – Hoan Nguyen Mar 17 '13 at 19:49
  • I edited my answer, change is onError method. remove the mIsListening = false; and add startListening. – Hoan Nguyen Mar 17 '13 at 19:52
0

start your recognition with : recognizeSpeechDirectly();

public void stopRecognition(){
      recognizer.destroy();
      recognizer = null;

}

public void onError(int error) {
      stopRecognition();
}

public void onResults(Bundle results){
      //Do something
      stopRecognition();
}

It works to fix "not connected to the recognition service" and "ERROR_RECOGNIZER_BUSY" errors

Bina
  • 21
  • 2