Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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?

like image 459
darkmatter Avatar asked Dec 22 '12 05:12

darkmatter


1 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:

  • Google Play: https://play.google.com/store/apps/details?id=be.lukin.android.babble
  • source code: https://github.com/lukin0110/babble/blob/master/android/app/src/be/lukin/android/babble/BabbleActivity.java
like image 107
Kaarel Avatar answered Nov 14 '22 00:11

Kaarel