7

当设备离线时,SpeechRecognizer 在 onResults 中返回 ERROR_NO_MATCH,而它在 onPartialResults() 回调中返回部分结果。上次我玩 SpeechRecognizer 时它离线工作得很好,我想知道是否有人找到了解决方案。

4

2 回答 2

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);
}
于 2015-06-05T16:08:11.310 回答
3

为了让答案更清楚一点,您需要先启用部分结果,并以特定方式调用 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() 现在会被多次调用,而 onError() 仍然会被 ERROR_NO_MATCH 调用。我最终使用了类似于此处列出的解决方案: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()
于 2016-01-21T19:12:06.127 回答