Nariman Ermekov
Nariman Ermekov

Reputation: 305

Android SpeechRecognizer. How to cach the "ready" sound event?

I have a little problem with android.speech.SpeechRecognizer method

speechRecognizer.startListening(speechIntent) 

Sometimes it takes a long time untill "ready" sound plays(mostly after app rerun). I cannot find something like onSpeechRecognitionReady listener. How can I catch this event to make a progressBar?

I init recognizer this way

private fun initSpeechRecognizer() {
        speechIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
        speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US")
        speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, packageName)
        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this)
        speechRecognizer.setRecognitionListener(object : RecognitionListenerAdapter(){
            override fun onResults(results: Bundle) {
                val matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
                tv_speech.text = matches[0]
                btn_speech.isChecked = false
            }
        })
    }

Upvotes: 0

Views: 313

Answers (2)

Alex
Alex

Reputation: 3382

I think you should use the RecognitionListener which has an onReadyForSpeech method so you could show your progressbar on init and hide it onReadyForSpeech

Upvotes: 1

Nariman Ermekov
Nariman Ermekov

Reputation: 305

Now i have to do this ugly hack. It produces redundant start listening and stop listening sounds.

override fun onCreate(savedInstanceState: Bundle?) {
    initSpeechRecognizer()            
    speechRecognizer.startListening(speechIntent)
    speechRecognizer.stopListening()
}

Upvotes: 0

Related Questions