2

I follow this question to do the offline speech on android.
I downloaded the language in google voice and it can work in offline.
The problem is that I want to know it's current running on offline or online speech, (just like Apple speech to text, there is an api to check for that) to display the speech stream in my app correctly
I wonder is there anyway to do that?
Here is my code:

    val intentSpeech =  Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
    intentSpeech.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US")
    intentSpeech = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
    intentSpeech.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
    intentSpeech.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
    intentSpeech.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
            packageName)
    val recognizer = SpeechRecognizer.createSpeechRecognizer(this)
    recognizer.setRecognitionListener(this)

P/s: I can see the Read Along app by google works perfectly in offline or online mode.
I'm trying to do the same with the android speech api. Is it possible?

ductran
  • 10,043
  • 19
  • 82
  • 165

1 Answers1

1

For offline speech to text, you can use Google's default STT model but it seems to be non-continuous.

private fun startSpeechToText() {
    val speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this)
    val speechRecognizerIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
    speechRecognizerIntent.putExtra(
        RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
    )
    speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())

    speechRecognizer.setRecognitionListener(object : RecognitionListener {
        override fun onReadyForSpeech(bundle: Bundle?) {}
        override fun onBeginningOfSpeech() {}
        override fun onRmsChanged(v: Float) {}
        override fun onBufferReceived(bytes: ByteArray?) {}
        override fun onEndOfSpeech() {}
        override fun onError(i: Int) {}

        override fun onResults(bundle: Bundle) {
            val result = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
            if (result != null) {
                // result[0] will give the output of speech
            }
        }           
        override fun onPartialResults(bundle: Bundle) {}
        override fun onEvent(i: Int, bundle: Bundle?) {}
    })  
    // starts listening ...
    speechRecognizer.startListening(speechRecognizerIntent)

}

If you don't want to use google as it requires to download the offline model for speech to text.

The other option for offline STT is the Vosk API as it has pretrained models in english and other language for live STT.

https://github.com/alphacep/vosk-android-demo

Reference: https://www.geeksforgeeks.org/offline-speech-to-text-without-any-popup-dialog-in-android/

rafi
  • 364
  • 3
  • 9