【发布时间】:2018-01-03 18:20:36
【问题描述】:
当设备离线时,SpeechRecognizer 在 onResults 中返回 ERROR_NO_MATCH,而它在 onPartialResults() 回调中返回部分结果。上次我玩 SpeechRecognizer 时它离线工作得很好,我想知道是否有人找到了解决方案。
【问题讨论】:
标签: android speech-recognition recognizer-intent
当设备离线时,SpeechRecognizer 在 onResults 中返回 ERROR_NO_MATCH,而它在 onPartialResults() 回调中返回部分结果。上次我玩 SpeechRecognizer 时它离线工作得很好,我想知道是否有人找到了解决方案。
【问题讨论】:
标签: android speech-recognition recognizer-intent
作为一种变通方法,我使用 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);
}
【讨论】:
为了让答案更清楚一点,您需要先启用部分结果,并以特定方式调用 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
简而言之:
【讨论】: