简体   繁体   中英

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM