Android, Kotlin. SpeechRecognizer is not working for some reason

Viewed 899

I'm working with android speech API called SpeechRecognizer trying to translate speech into text, but for some reason, as soon as I click on the button - I see the message "CAN'T REACH GOOGLE AT THE MOMENT", and the window closes without waiting for my speech. Or the sound just stutters and nothing happens.

class MainActivity : AppCompatActivity() {
    var voiceButton: Button? = null
    var textView: TextView? = null
    var listener: Listener? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initializeView()
        initializeListeners()
    }

    fun initializeView() {
        voiceButton = findViewById(R.id.voiceButton)
        textView = findViewById(R.id.textView)
        listener = Listener()
    }
    fun initializeListeners() {
        voiceButton?.setOnClickListener {
            val speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this)
            speechRecognizer.setRecognitionListener(listener)
            val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);

            speechRecognizer.startListening(intent)

            if (intent.resolveActivity(packageManager) != null) {
                startActivityForResult(intent, 10)
            } else {
                Toast.makeText(this, "Unfortunately device not supported", Toast.LENGTH_SHORT).show()
            }
        }
    }

Just why? What's wrong with this code? I think this code should work because for other people it works fine. Maybe my emulator (Nexus One API 22) is not supported for this API? I just don't know what's the problem.

Maybe you do know how to solve it? Thanks, i'll be grateful for your help!

IF SOMEBODY SUDDENLY FACES A SIMILAR PROBLEM, THAT'S HOW I SOLVE THIS PROBLEM AFTER A WHILE:

You need to turn on Virtual microphone uses host audio input. Go to: More ("..." on the right side) -> Microphone -> Turn on "Virtual microphone uses host audio input". Also, your emulator must have a later version of the update, otherwise speech recognition will not work.

1 Answers

I think your listener is the problem, you cant just initialize listener = Listener() and pass it to the speechRecognizer.It needs to receive a RecognitionListener type of listener which has many callbacks that you can use like onResults which indicates every time your listener catches voice speeched and more callbacks. try changing your listener to listener = object : RecognitionListener{ HERE OVERRIDE THE METHODS } eventually it will looks like that:

     listener = object :RecognitionListener {
            override fun onReadyForSpeech(params: Bundle?) {
                TODO("Not yet implemented")
            }

            override fun onRmsChanged(rmsdB: Float) {
                TODO("Not yet implemented")
            }

            override fun onBufferReceived(buffer: ByteArray?) {
                TODO("Not yet implemented")
            }

            override fun onPartialResults(partialResults: Bundle?) {
                TODO("Not yet implemented")
            }

            override fun onEvent(eventType: Int, params: Bundle?) {
                TODO("Not yet implemented")
            }

            override fun onBeginningOfSpeech() {
                TODO("Not yet implemented")
            }

            override fun onEndOfSpeech() {
                TODO("Not yet implemented")
            }

            override fun onError(error: Int) {
                TODO("Not yet implemented")
            }

            override fun onResults(results: Bundle?) {
                TODO("Not yet implemented")
            }
        }

you can read about any callback to make use of your own needs hope it helps

Related