SpeechRecognizer 在启动后立即不匹配

SpeechRecognizer gives no match immediately after the start

我正在尝试在我的 android 应用程序中使用 SpeechRecognizer 库,到目前为止它的工作给我留下了疑问。首先,当我停止说话时它不会停止。 如果我试图停止语音识别自己,下次它会立即给我 'No match!'。

我的问题是:当我使用 google 语音识别时(例如,当我在网络上搜索时),它非常有效。在我的应用程序中,它远非完美,尽管库是一样的。我的实现有什么问题?

我的代码(简化版):

注意:我尝试使用部分结果使语音识别更灵活,但除了识别速度变快了一点,我看不出任何效果。

    public void setupVoiceRecognition(Activity activity) {
        mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(activity.getApplicationContext());
        mSpeechRecognizer.setRecognitionListener(this);

        mRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                activity.getPackageName());
        mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,
                true);
        mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        mRecognizerIntent.putExtra(EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 5000);
        mRecognizerIntent.putExtra(EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 3000);
        mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
        mContext = activity.getApplicationContext();
        if (mMainBtn != null) {
            mMainBtn.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View view) {
                    VoiceRecognition.this.onClick();
                }
            });
        }
    }    

    public void forceStop() {
        if (mListening) {
            toggleListening(false);
        }
    }    

    public void onClick() {            
        toggleListening(!mListening);            
    }


    private void toggleListening(boolean start) {
        mPartialLength = 0;
        if (start) {
            mSpeechRecognizer.startListening(mRecognizerIntent);
        } else {
            mSpeechRecognizer.stopListening();
        }
        if (mMainBtn != null) {
            mMainBtn.setImageResource((start) ? R.drawable.icon_record_active : R.drawable.icon_record_white);
        }
        if (mSupportBtn != null) {
            mSupportBtn.setImageResource((start) ? R.drawable.icon_record_active : R.drawable.icon_record_white);
        }
        mListening = start;
    }

   ...

    @Override public void onError(int i) {
        if (mListening) {
            String errorText;
            switch (i) {
                case SpeechRecognizer.ERROR_AUDIO:
                    errorText = MyApp.getContext().getString(R.string.speech_recognition_err3);
                    break;

                ...

            }
            MyApp.showToast(errorText);
            toggleListening(false);
            if (i == NO_MATCH)  {
                toggleListening(true);
            }
        }
    }

    @Override public void onResults(Bundle bundle) {
        ArrayList<String> matches = bundle
                .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        if (matches != null) {
            String fullText = mViewForText.getText().toString();
            mViewForText.setText(fullText.substring(0, fullText.length() - mPartialLength) + matches.get(0) + " ");
            mViewForText.requestFocus(View.FOCUS_RIGHT);
            mViewForText.setSelection(mViewForText.getText().length());
            mPartialLength = 0;
            forceStop();
        }
    }

    @Override public void onPartialResults(Bundle bundle) {
        ArrayList<String> matches = bundle
                .getStringArrayList(EXTRA_PARTIAL_RESULTS);
        if (matches != null) {
            mViewForText.setText(mViewForText.getText().toString() + matches.get(0) + " ");
            mPartialLength +=  matches.get(0).length() + 1;
            mViewForText.requestFocus(View.FOCUS_RIGHT);
            mViewForText.setSelection(mViewForText.getText().length());
        }
    }
}

Google 通过第三方应用程序的 SpeechRecognizer 禁用连续语音识别。我认为这是因为他们现在已经支付了 API (https://cloud.google.com/speech/),效果很好但不是免费的。

关于 NO_MATCH 错误。 Google 听到自己的嘟嘟声邀请信号并假设这是演讲开始。比如那个嘟嘟声无法识别returnsNO_MATCH错误

有一个选项。您可以降级 Google 应用程序以获得更稳定的识别服务工作。 Google 应用程序的最后一个正常工作版本是 6.2.34