12

I've looked at the Android example for VoiceRecognition, but I don't really understand what it is suppose to do or how it works. In the manifest there isn't any sort of main activity to run and so when I install the app on my phone I can't run it.

I'm also trying to find a simple example of Speech to text that takes speech as input and outputs the text on the screen. Just so I can study it to see how it works, but I haven't been able to find any sort of example on the web that shows it.

Palagerini
  • 159
  • 1
  • 1
  • 6

1 Answers1

12

I did it like that:

in onCreate:

List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);

In the method starting the voice recognition:

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo...");
startActivityForResult(intent, REQUEST_CODE);

onActivityResult:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
    {
        // Populate the wordsList with the String values the recognition engine thought it heard
        ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
    }
}

Hope I'm not missing anything, been a time since. Drop me a note if something doesn't work. About the text output: I'm sure you can handle that once you have a populated matches array.

Cdr. Powell
  • 722
  • 6
  • 19
  • Simple step to integrate the Speech to Text in android with source code. Use the below link http://viralpatel.net/blogs/android-speech-to-text-api/ – Dhamodharan Oct 29 '12 at 09:12
  • PackageManager pm = getPackageManager(); – Cdr. Powell Mar 16 '15 at 09:17
  • As far as I know this is dependant on the user settings. I haven't tried it but please have a look at [this SO-Question/Answer](http://stackoverflow.com/questions/17616994/offline-speech-recognition-in-android-jellybean). The answer was created before Google introduced offline voice recognition, I don't know if the code needs to be changed to support it. – Cdr. Powell Jul 13 '15 at 06:21
  • Why is the snippet in onCreate neccessary? – Sam Jul 12 '22 at 00:39
  • What is REQUEST_CODE? – Sam Jul 12 '22 at 00:41