SpeechRecognizer直接给出onError 5,然后给出7。无需等待几秒钟的输入

问题描述 投票:1回答:1

我有以下代码来启动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()
}

这通常有效,但是有时我会陷入一个怪异的循环,就像这样:

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

为什么我会收到onError 5(ERROR_CLIENT),以及为什么它开始监听后会自动得到7(NO_MATCH)还有可能使它等待至少4-5秒再抛出onEndOfSpeech吗?

android speech-recognition voice-recognition voice speech
1个回答
0
投票
我也遇到过同样的问题。

使用.cancel()而不是.stopListening()将对其进行修复。如果在此之后,它仍然会发生。您可能需要升级Google应用。

© www.soinside.com 2019 - 2024. All rights reserved.