'SpeechRecognizer directly gives onError 5 and then 7. Without waiting a couple of seconds for input

I have the following code to start the SpeechRecognizer:

  fun startVoiceRecognitionActivityNoUI(value: VOICE_COMMANDS) {
    Log.i(TAG, "SPEECH Results startVoiceRecognitionActivityNoUI")
    if (speechRecognizer == null) {
        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this)
        speechRecognizer?.setRecognitionListener(object : RecognitionListener {
            override fun onReadyForSpeech(params: Bundle?) {
                FL.i(TAG, "SPEECH Results onReadyForSpeech")
                var beep = BeepToneGenerator()
                beep.playTone(this@MainActivity, ToneGenerator.TONE_PROP_BEEP)
            }

            override fun onRmsChanged(rmsdB: Float) {
                if (dialogDialUserCountDown != null) {
                    dialogDialUserCountDown!!.setRMSMarin(30 - 3 * rmsdB)
                }
                if (XelionDialerManager.dialogPhone != null) {
                    XelionDialerManager.dialogPhone!!.setRMSMarin(30 - 3 * rmsdB)
                }
            }

            override fun onBeginningOfSpeech() {
                FL.i(TAG, "SPEECH Results onBeginningOfSpeech")
                if (dialogDialUserCountDown != null) {
                    dialogDialUserCountDown!!.setRMSMarin(30f)
                }
            }

            override fun onEndOfSpeech() {
                FL.i(TAG, "SPEECH Results onEndOfSpeech")
                if (dialogDialUserCountDown != null) {
                    dialogDialUserCountDown!!.setRMSMarin(30f)
                }
            }

            override fun onBufferReceived(buffer: ByteArray?) {}
            override fun onError(error: Int) {
                FL.i(TAG, "SPEECH Results onError: $error")
                willRetryToCall()
            }

            override fun onEvent(eventType: Int, params: Bundle?) {
                FL.i(TAG, "SPEECH Results onEvent")
            }

            override fun onPartialResults(partialResults: Bundle?) {
                Log.i(TAG, "SPEECH Results onPartialResults")
                var data = partialResults?.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
                var unstableData = partialResults?.getStringArrayList("android.speech.extra.UNSTABLE_TEXT")
                var matches = ArrayList<String>()
                if (data != null) {
                    data.forEachIndexed { index, element ->
                        matches.add(data.get(index) + unstableData?.get(index))
                    }
                    if (matches != null) {
                        Log.i(TAG, "SPEECH Results onPartialResults are: " + matches.toString())
                    } else Log.i(TAG, "SPEECH Results onPartialResults are NULL")
                    handleVoiceRecogMatches(matches)
                }
                FL.i(TAG, "SPEECH Results onPartialResults")
            }

            override fun onResults(results: Bundle?) {
                var matches = results?.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
                if (matches != null) {
                    FL.i(TAG, "SPEECH Results are: " + matches.toString())
                } else FL.i(TAG, "SPEECH Results are NULL")
                handleVoiceRecogMatches(matches)
            }
        })
    } else {
        speechRecognizer?.stopListening()
    }
    startListening()
    voiceCommand = value
}

private fun startListening() {
    val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
    speechRecognizer?.startListening(intent)
    object : CountDownTimer(10000, 10000) {
        override fun onTick(millisUntilFinished: Long) {}

        override fun onFinish() {
            speechRecognizer?.stopListening()
        }
    }.start()
}

This mostly works, but sometimes I get stuck in a weird cycle, like this:

2019-12-23 16:33:51.604 19411-19411/com.xelion.android.debug I/MainActivity: SPEECH Results onError: 5
2019-12-23 16:33:51.605 19411-19411/com.xelion.android.debug I/MainActivity: SPEECH Results onReadyForSpeech
2019-12-23 16:33:52.358 19411-19411/com.xelion.android.debug I/MainActivity: SPEECH Results onBeginningOfSpeech
2019-12-23 16:33:53.159 19411-19411/com.xelion.android.debug I/MainActivity: SPEECH Results onEndOfSpeech
2019-12-23 16:33:53.317 19411-19411/com.xelion.android.debug I/MainActivity: SPEECH Results onError: 7
2019-12-23 16:33:53.319 19411-19411/com.xelion.android.debug I/MainActivity: SPEECH Results willRetryToCall TRUE?
2019-12-23 16:33:53.320 19411-19411/com.xelion.android.debug I/Xelion_Android: MainActivity: SPEECH Results startVoiceRecognitionActivityNoUI
2019-12-23 16:33:53.326 19411-19411/com.xelion.android.debug I/MainActivity: SPEECH Results onError: 5
2019-12-23 16:33:53.347 19411-19411/com.xelion.android.debug I/MainActivity: SPEECH Results onReadyForSpeech
2019-12-23 16:33:53.896 19411-19411/com.xelion.android.debug I/MainActivity: SPEECH Results onBeginningOfSpeech
2019-12-23 16:33:54.681 19411-19411/com.xelion.android.debug I/MainActivity: SPEECH Results onEndOfSpeech
2019-12-23 16:33:54.897 19411-19411/com.xelion.android.debug I/MainActivity: SPEECH Results onError: 7
2019-12-23 16:33:54.898 19411-19411/com.xelion.android.debug I/MainActivity: SPEECH Results willRetryToCall TRUE?
2019-12-23 16:33:54.899 19411-19411/com.xelion.android.debug I/Xelion_Android: MainActivity: SPEECH Results startVoiceRecognitionActivityNoUI
2019-12-23 16:33:54.915 19411-19411/com.xelion.android.debug I/MainActivity: SPEECH Results onError: 5
2019-12-23 16:33:54.932 19411-19411/com.xelion.android.debug I/MainActivity: SPEECH Results onReadyForSpeech

Why do I get the onError 5 (ERROR_CLIENT) and why after it starts to listen, it automatically gets 7 (NO_MATCH) Also is it possible to make it so that it waits for atleast 4-5 seconds before throwing onEndOfSpeech?



Solution 1:[1]

I got the same problem.

To use .cancel() instead of .stopListening() will fix it. If after this, it still happens. You may need to upgrade Google app.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1