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
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;
..........
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With