Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restart listening again after RecognitionListener take ERROR_RECOGNIZER_BUSY error

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

like image 849
odincer Avatar asked Mar 16 '13 18:03

odincer


Video Answer


1 Answers

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;
          ..........
    }
like image 189
Hoan Nguyen Avatar answered Sep 30 '22 17:09

Hoan Nguyen