我正在改进一个使用 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){
}
}
这是我开始收听的代码:
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);
}
我想在出现 ERROR_RECOGNIZER_BUSY 时重新开始收听,
另一个人在 stackoverflow 上提到了这个错误,但我不清楚,无法实现。
How to handle ERROR_RECOGNIZER_BUSY
提前致谢
最佳答案
您有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);
}
当图标被点击时
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;
..........
}
关于android - RecognitionListener出现ERROR_RECOGNIZER_BUSY错误后如何重新开始监听,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15453291/