添加EXTRA_PARTIAL_RESULTS时,SpeechRecognizer停止工作

时间:2018-09-11 08:28:47

标签: android

here相同的问题,但对我来说仍然没有解决。

我的SpeechRecognizer可以正常工作,直到我添加EXTRA_PARTIAL_RESULTS为止。在这种情况下,onPartialResults会很快触发一次(我不会说完),识别器停止并返回matches长度为1的空字符串。

如果我删除EXTRA_PARTIAL_RESULTS行,那么我会说完整的句子,onResults被解雇,一切都会按预期进行。

有什么想法吗?

@Override
public void onStart() {
    Log.d(TAG, "onStart");
    super.onStart();

    // set up speech recognizer
    mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(getContext());
    mSpeechRecognizer.setRecognitionListener(this);

}

private void startSpeechRecognition() {
    Log.d(TAG, "start speech recognition fn");
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.US.toString());
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getActivity().getApplication().getPackageName());
    intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);

    mSpeechRecognizer.startListening(intent);
    mRecognitionOn = true;
}

@Override
public void onResults(Bundle bundle) {
    Log.d(TAG, "results");
    this.onPartialResults(bundle);
    doSomething();
}

@Override
public void onPartialResults(Bundle bundle) {
    Log.d(TAG, "partial results");
    ArrayList<String> matches = bundle.getStringArrayList(
            SpeechRecognizer.RESULTS_RECOGNITION);
    Log.d("debug1", String.valueOf(matches.size()));
    Log.d("debug1", "this is the string: " + matches.get(0));
    mAnswerEditText.setText(matches.get(0));
}

0 个答案:

没有答案