SpeechRecognizer在开始后不立即给出匹配

时间:2017-12-07 12:39:15

标签: java android speech-recognition

我试图在我的Android应用程序中使用SpeechRecognizer库,到目前为止,它的工作给我留下了问题。首先,当我停止说话时它不会停止。 如果我试图停止言语识别自己,下次它会给我“不匹配!”#39;马上。

我的问题是:当我使用谷歌语音识别时(例如,当我在网上搜索时),它就像一个魅力。在我的应用程序中,它远非完美,尽管库是相同的。我的实施有什么问题?

我的代码(简化):

注意:我尝试使用部分结果来使语音识别更加灵活,但除了识别变得更快之外我无法看到任何效果。

    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());
        }
    }
}

1 个答案:

答案 0 :(得分:2)

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

关于NO_MATCH错误。谷歌听到自己的嘟嘟邀请信号,并假设它是语音开始。如发出蜂鸣声无法识别,则返回NO_MATCH错误。

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