Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speech recognition in Android

Tags:

java

android

I am working on speech recognition and need some sample programs.

Can anyone guide me?

like image 942
bharathi Avatar asked Jun 15 '10 05:06

bharathi


2 Answers

Let me cut and paste a bit to show you what code you will need.

EDIT: you can also download a handy abstract class from this project.

You will need this intent (parameterize as you see fit):

public Intent getRecognizeIntent(String promptToUse, int maxResultsToReturn)
{
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxResultsToReturn);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, promptToUse);
    return intent;
}

Then you need to send your intent to the speech recognition activity like so,

public void gatherSpeech(String prompt)
{
    Intent recognizeIntent = getRecognizeIntent(prompt);
    try
    {
        startActivityForResult(recognizeIntent, SpeechGatherer.VOICE_RECOGNITION_REQUEST_CODE);
    }
    catch (ActivityNotFoundException actNotFound)
    {
        Log.w(D_LOG, "did not find the speech activity, not doing it");
    }
}

Then you will need to have your activity handle the speech result:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    Log.d("Speech", "GOT SPEECH RESULT " + resultCode + " req: "
        + requestCode);
    if (requestCode == SpeechGatherer.VOICE_RECOGNITION_REQUEST_CODE)
    {
        if (resultCode == RESULT_OK)
        {
            ArrayList<String> matches = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            Log.d(D_LOG, "matches: ");
            for (String match : matches)
            {
                Log.d(D_LOG, match);
            }
        }
    }
}
like image 65
gregm Avatar answered Oct 11 '22 09:10

gregm


For more info see:

http://developer.android.com/resources/articles/speech-input.html

http://developer.android.com/reference/android/speech/RecognizerIntent.html

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.html

like image 39
Michael Levy Avatar answered Oct 11 '22 09:10

Michael Levy