Home » Android » How do you use Android's real time speech to text?

How do you use Android's real time speech to text?

Posted by: admin May 14, 2020 Leave a comment

Questions:

In android 4.1 you can get real-time speech to text conversion using the microphone option on the keyboard.

I’ve been looking at the docs for android.speech trying to find out how to implement real-time speech to text for an application. However, the only option that would facilitate this is the “EXTRA_PARTIAL_RESULTS” option (Which the server ignores every time I try to use it).

The code:

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "VoiceIME");
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 3000L);

mSpeaker.startListening(intent);

Never returns partial results.

I know this is possible since the keyboard version does it consistently. Anyone know how?

How to&Answers:

Before you call startListening you need to register the onPartialResults-callback. Two important things to note:

  • the structure of the bundle with which onPartialResults is called is not specified by the Android API;
  • not every speech recognizer supports this callback.

So your code will be specific to Google Voice Search.

mSpeaker.setRecognitionListener(new RecognitionListener() {
  ...
  public void onPartialResults(Bundle partialResults) {
    // WARNING: The following is specific to Google Voice Search
    String[] results = 
      partialResults.getStringArray("com.google.android.voicesearch.UNSUPPORTED_PARTIAL_RESULTS");
    updateTheUi(results);
  }
  ...
}

To see this callback in action in an open source app, see Babble:

Answer:

if you want real-time partial results to show up while the mic is on , while the speaker is speaking, you might want to drop approach using recognizerIntent and drop recognitionService in favor of a simple android text box combined with prior selection of the ‘mic’ icon like you can do in the android ‘notes’ sample app…

see ./samples/android-16/NotePad/tests/src/com/example/android/notepad

This combo provides the feature like u see in real time the partial text-from-speech results as they come back from the server-side ‘voiceSearch’ which is somehow different from ‘recognizer’ regarding the ‘partial’ callback.

Numerous comments state that recognizerIntent does not fire a callback to ‘onPartialResults’. For some reason, android 4.2 does not seem to support the ‘continuous’ speechRecognition mode that works fine using javascript. My tests of ‘RecognitionListener’ interface on 4.2 show hundreds of callbacks to ‘onRmsChanged’ on volume events , but zero activity on the ‘partialResult’ event. Somewhere , this callback gets lost??

for js solution, install chrome-beta release 25 and go here

using android notes app. sample and pre-selecting the mic icon from the keyboard, you can do exactly the same thing as the JS webapp link above.

Answer:

Since we cannot know for sure the key names of the Bundle coming in from partial results callback, use this to find out its contents:

public void onPartialResults(Bundle partialResults) {    
    String string = "Bundle{";
    for (String key : partialResults.keySet()) {
        string += " " + key + " => " + partialResults.get(key) + ";";
    } 
    Log.e("joshtag","onPartialResults"+string);
    //see the keynames in Logcat and extract partial reesults here
}