SpeechRecognizer离线ERROR_NO_MATCH

时间:2015-06-04 21:08:31

标签: android speech-recognition recognizer-intent

当设备离线时,SpeechRecognizer会在onResults中返回ERROR_NO_MATCH,同时返回onPartialResults()回调中的部分结果。我最后一次使用SpeechRecognizer时它离线工作正常,我想知道是否有人找到了解决方案。

2 个答案:

答案 0 :(得分:5)

作为一种解决方法,我使用onPartialResults()中返回的partialResults。 在返回的包中,“SpeechRecognizer.RESULTS_RECOGNITION”包含所有术语减去最后一个术语,“android.speech.extra.UNSTABLE_TEXT”包含最后一个缺失的术语。

    @Override
public void onPartialResults(Bundle partialResults) {
    ArrayList<String> data = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    ArrayList<String> unstableData = partialResults.getStringArrayList("android.speech.extra.UNSTABLE_TEXT");
    mResult = data.get(0) + unstableData.get(0);
}

答案 1 :(得分:2)

为了使答案更加清晰,您需要首先启用部分结果,并以特定方式调用UNSTABLE_TEXT:

// When creating the intent, set the partial flag to true
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,true); 

// When requesting results in onPartialResults(), the UNSTABLE_TEXT parameter to getSTtringArrayList() must be in quotes
ArrayList<String> unstableMatches = partialResults.getStringArrayList("android.speech.extra.UNSTABLE_TEXT");
现在多次调用

onPartialResults(),并且仍然使用ERROR_NO_MATCH调用onError()。我最终使用的解决方案类似于此处列出的解决方案:https://github.com/nenick/QuAcc/blob/master/app/src/main/java/de/nenick/quacc/speechrecognition/speech/RecognizerListenerWithOfflineWorkaround.java

简而言之:

  • 跟踪部分结果以及是否显示错误
  • 在onBeginningOfSpeech()
  • 中重置两者
  • 调用onPartialResults()时,将部分结果存储在变量中
  • 当onError()被调用时,检查结果是否为ERROR_NO_MATCH,并将SpeechRecognizer.RESULTS_RECOGNITION与“android.speech.extra.UNSTABLE_TEXT”结合到您的部分结果变量中
  • 调用onResults()