开发者

Speech recognition in Android

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

Can anyone guide 开发者_开发知识库me?


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);
            }
        }
    }
}


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


First need to Showing google speech input dialog like so

 /**
 * Showing google speech input dialog
 * */
private void promptSpeechInput() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
            getString(R.string.speech_prompt));
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
}

then need to receiving speech input like so.

 /**
 * Receiving speech input
 * */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case REQ_CODE_SPEECH_INPUT: {
            if (resultCode == RESULT_OK && null != data) {

                ArrayList<String> result = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                txtSpeechInput.setText(result.get(0));
            }
            break;
        }

    }
}

and need to set REQ_CODE_SPEECH_INPUT

  private final int REQ_CODE_SPEECH_INPUT = 100;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜