SpeechRecognizer throws onError on the first listening

后端 未结 5 939
[愿得一人]
[愿得一人] 2020-12-11 07:09

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_

5条回答
  •  不思量自难忘°
    2020-12-11 07:38

    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.
    }
    

提交回复
热议问题