如何使语音识别永远听?

时间:2020-06-01 09:19:19

标签: android kotlin speech-recognition

我在这里有一个程序,这是一个单击按钮即可调用的函数。我希望语音识别能够继续识别语音,直到调用stoplistening()为止。我不断收到错误代码6、7,每当我说一声沉默几秒钟后,它停止监听时,我甚至定义了沉默和输入值:

 listenerintent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS , 200000)
            listenerintent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS , 5000000)

,但如果在静音前说了几秒钟,则应用程序在静音2秒后仍会关闭。如果什么也没说,那么语音识别会监听我上面定义的最大时间。

我希望语音识别功能能够被激活,而不管识别出某些内容后是否保持沉默。我缺少能解决此问题的任何参数吗?还是该库无法永久运行并且必须在识别出某些内容后停止?我还有另外一个终点指针吗?

 var recogobj = SpeechRecognizer.createSpeechRecognizer(this)
        var listenerintent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
        listenerintent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS , 200000)
        listenerintent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS , 5000000)
        listenerintent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, 5000000)



        val recoglistener = object : RecognitionListener {
            override fun onReadyForSpeech(params: Bundle?) {

            }

            override fun onRmsChanged(rmsdB: Float) {


            }

            override fun onBufferReceived(buffer: ByteArray?) {

            }

            override fun onPartialResults(partialResults: Bundle?) {
                    Toast.makeText(this@Recordinit , RecognizerIntent.EXTRA_PARTIAL_RESULTS , Toast.LENGTH_LONG).show()
                var data = partialResults!!.getStringArray(RecognizerIntent.EXTRA_PARTIAL_RESULTS)
                for (result in data.orEmpty()){


                if (result == "test") {
                    recogobj.stopListening()
                    Toast.makeText(this@Recordinit, "ok ", Toast.LENGTH_SHORT).show()
                }
                }
            }

            override fun onEvent(eventType: Int, params: Bundle?) {

            }

            override fun onBeginningOfSpeech() {


            }

            override fun onEndOfSpeech() {



            }

            override fun onError(error: Int) {
                if (error ==6 || error == 7){
                    recogobj.startListening(listenerintent)
                }
                else{
                    Toast.makeText(this@Recordinit ,"${error}" , Toast.LENGTH_LONG).show()
                }





            }

            override fun onResults(results: Bundle?) {




            }

        }
        recogobj.setRecognitionListener(recoglistener)

            recogobj.startListening(listenerintent)

    }

0 个答案:

没有答案