我正在改进一个使用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
提前致谢
答案 0 :(得分: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);
}
点击图标时
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;
..........
}
答案 1 :(得分:0)
开始认可:recognSpeechDirectly();
public void stopRecognition(){
recognizer.destroy();
recognizer = null;
}
public void onError(int error) {
stopRecognition();
}
public void onResults(Bundle results){
//Do something
stopRecognition();
}
它可以修复“未连接到识别服务”和“ERROR_RECOGNIZER_BUSY”错误