我目前有一个语音识别器在听声音,它是通过一个分别激活startListening()和stopListening()的按钮激活的。我的问题是在第一次之后stopListening()并没有真正停止SpeechRecognizer。使用按钮激活和禁用SpeechRecognizer时,您可以清晰地听到独特的声音。但是,在第一次(成功)之后调用stopListening()之后,不会产生结束声音,并且结束声音的唯一方法是超时。

我像这样创建了SpeechRecognizer:

mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                this.getPackageName());

        mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        mSpeechRecognizer.setRecognitionListener(this);


我重写了所有必要的方法:

@Override
    public void onReadyForSpeech(Bundle params) {
        Log.i(TAG, "Ready for speech input");
    }

    @Override
    public void onBeginningOfSpeech() {
        mIsEndOfSpeech = false;
        Log.i(TAG, "Speech beginning");
    }

    @Override
    public void onRmsChanged(float rmsdB) {
        Log.i(TAG, "Speech RMS Changed: " + rmsdB);
    }

    @Override
    public void onBufferReceived(byte[] buffer) {
        Log.i(TAG, "Speech buffer received: " + buffer.toString());
    }

    @Override
    public void onEndOfSpeech() {
        mIsEndOfSpeech = true;
        Log.i(TAG, "Speech recognition ended");
    }

    @Override
    public void onError(int error) {
        Log.e(TAG, "Speech recognition error: " + error);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mSpeechRecognizer != null) {
            mSpeechRecognizer.destroy();
            Log.i(TAG, "Speech destroy");
        }
    }

    @Override
    public void onResults(Bundle results) {
        Log.i(TAG, "Speech recognition results received");

        ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    }

    @Override
    public void onPartialResults(Bundle partialResults) {
        Log.i(TAG, "Speech recognition partial results received");
    }

    @Override
    public void onEvent(int eventType, Bundle params) {
        Log.i(TAG, "Speech recognition event called: " + eventType);
    }


我的问题在于,调用startListening()和stopListening()的第一次尝试成功。但是,此后的任何尝试,stopListening()均不起作用,并且必须超时才能结束。我注意到的一件事是,在LogCat中,它声明“即使已调用startListening(),也不能在没有startListening()的情况下调用stopListening()。

最佳答案

假设Google是您的默认识别服务提供商,则当前版本的Google'Now'stopListening()无法正常工作。

请参阅this thread for more information和其他相关错误。

如果要测试以确认问题,该问题已在most recent beta release中修复。仍然存在其他一些错误。

关于android - SpeechRecognizer stopListening()无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39183759/

10-12 06:16