SpeechRecognizer抛出的onError在第一监听 [英] SpeechRecognizer throws onError on the first listening

查看:158
本文介绍了SpeechRecognizer抛出的onError在第一监听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的5我面临着奇怪的问题。到 startListening SpeechRecognizer的第一次调用的结果给onError的错误code 7( ERROR_NO_MATCH )。

In the Android 5 I faced with strange problem. The first call to the startListening of SpeechRecognizer results to the onError with error code 7 (ERROR_NO_MATCH).

我做了测试应用程序具有以下code:

I made test app with the following code:

if (speechRecognizer == null) {

    speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
    speechRecognizer.setRecognitionListener(new RecognitionListener() {
        @Override
        public void onReadyForSpeech(Bundle bundle) {
            Log.d(TAG, "onReadyForSpeech");
        }

        @Override
        public void onBeginningOfSpeech() {
            Log.d(TAG, "onBeginningOfSpeech");
        }

        @Override
        public void onRmsChanged(float v) {
            Log.d(TAG, "onRmsChanged");
        }

        @Override
        public void onBufferReceived(byte[] bytes) {
            Log.d(TAG, "onBufferReceived");
        }

        @Override
        public void onEndOfSpeech() {
            Log.d(TAG, "onEndOfSpeech");
        }

        @Override
        public void onError(int i) {
            Log.d(TAG, "onError " + i);
        }

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

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

        @Override
        public void onEvent(int i, Bundle bundle) {
            Log.d(TAG, "onEvent");
        }
    });
}

final Intent sttIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en");
sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");

speechRecognizer.startListening(sttIntent);

和后的第一次有这样的日志信息 startListening 电话:

And have this log messages after first startListening call:

onError 7
onReadyForSpeech
onBeginningOfSpeech
onEndOfSpeech
onResults

和下层出不穷的消息 startListening 呼叫:

And following messages after another startListening calls:

onRmsChanged
...
onRmsChanged
onReadyForSpeech
onRmsChanged
...
onRmsChanged
onBeginningOfSpeech
onRmsChanged
...
onRmsChanged
onEndOfSpeech
onRmsChanged
onRmsChanged
onRmsChanged
onResults

那么,什么是这个错误的原因以及如何解决呢?

So, what is the reason of this error and how do I fix it?

推荐答案

我有同样的问题,但我无法找到一个解决办法,所以我最终只是调用返回内部的onError如果startListening和onError的之间的时间是不合理的短

I had the same problem but I couldn't find a workaround, so I ended up just calling return inside onError if the time between startListening and onError is unreasonably short.

protected long mSpeechRecognizerStartListeningTime = 0;

protected synchronized void speechRecognizerStartListening(Intent intent) {
    if (mSpeechRecognizer != null) {
        this.mSpeechRecognizerStartListeningTime = System.currentTimeMillis();
        RLog.d(this, "speechRecognizerStartListening");
        this.mSpeechRecognizer.startListening(intent);
    }
}
...
@Override
public synchronized void onError(int error) {
    RLog.i(this, this.hashCode() + " - onError:" + error);

// Sometime onError will get called after onResults so we keep a boolean to ignore error also
    if (mSuccess) {
        RLog.w(this, "Already success, ignoring error");
        return;
    }

    long duration = System.currentTimeMillis() - mSpeechRecognizerStartListeningTime;
    if (duration < 500 && error == SpeechRecognizer.ERROR_NO_MATCH) {
        RLog.w(this, "Doesn't seem like the system tried to listen at all. duration = " + duration + "ms. This might be a bug with onError and startListening methods of SpeechRecognizer");
        RLog.w(this, "Going to ignore the error");
        return;
    }

// -- actual error handing code goes here.
}

这篇关于SpeechRecognizer抛出的onError在第一监听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆