如何在RecognitionListener出现ERROR_RECOGNIZER_BUSY错误后再次重新开始侦听

odi*_*cer 6 android speech-recognition

我正在改进一个使用RecognitionListener类来监听用户语音的Android应用程序,这里我得到以下结果:

1)如果麦克风图标,用户点击说了句一切都很好2-)如果麦克风图标,用户点击再次点击麦克风图标或没有说什么,我得到的onerror 和错误类型是: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){
  }

}
Run Code Online (Sandbox Code Playgroud)

这是我开始收听的代码:

 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);

     }
Run Code Online (Sandbox Code Playgroud)

我想在出现ERROR_RECOGNIZER_BUSY时重新开始收听,

另一个人在stackoverflow上讲述了这个错误,但对我来说并不清楚,也无法实现它.

如何处理ERROR_RECOGNIZER_BUSY

提前致谢

Hoa*_*yen 1

您出现ERROR_RECOGNIZER_BUSY是因为当用户单击按钮并再次单击时,您会调用startListening两次。更改您的代码如下:

// 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);
}
Run Code Online (Sandbox Code Playgroud)

当点击图标时

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;
          ..........
    }
Run Code Online (Sandbox Code Playgroud)