开发者

How do I use voice search and VoiceRecognition on Android?

I want to use VoiceRecognition in my application, but this application needs to install voice search.

I don't want the user to have to install another other application then return to my application to run it. I want voice search to be installed from my application, or alternatively I'd like to find a tutorial to on how to add Voice Search capability to my appl开发者_开发百科ication.

What can I do?


Use the RecognizerIntent to fire the speech recognizer installed on your device


This can be done in a few simple steps:

  1. Create some sort of button in your activity, and place the following code in its OnClickListener:

    // Define MY_REQUEST_CODE as an int constant in your activity...I use ints in the 10000s startVoiceRecognitionActivity(MY_REQUEST_CODE, "Say something.");

  2. Override the onActivityResult() method in your activity. In the implementation, place a switch block or if statement to run some logic when the requestCode argument matches your MY_REQUEST_CODE constant. Logic similar to the following will get you the list of results the speech recognition activity thought it heard:

    ArrayList keywordMatches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

  3. You may get 0 or many matches from the recognizer. Be sure to handle all cases.

  4. In some cases, the speech recognizer may not even be on the device. Try to handle that where you call startVoiceRecognitionActivity().


I found this tutorial :

http://www.jameselsey.co.uk/blogs/techblog/android-how-to-implement-voice-recognition-a-nice-easy-tutorial/

hope this helps.


Android Open Source VoiceRecognition Example


Here is a Simple way to Handle Voice Search

Step 1 Call this method on button click

public void startVoiceRecognition() {
    Intent intent = new Intent("android.speech.action.RECOGNIZE_SPEECH");
    intent.putExtra("android.speech.extra.LANGUAGE_MODEL", "free_form");
    intent.putExtra("android.speech.extra.PROMPT", "Speak Now");
    this.mContainerActivity.startActivityForResult(intent, 3012);
}

Step 2 Override onActivityResult method

@ Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 3012 && resultCode == RESULT_OK) {
        ArrayList < String > matches = data.getStringArrayListExtra("android.speech.extra.RESULTS");
        String result= matches.get(0);
        //Consume result 
        edittext.setText(result);
    }
}

Thats all, DONE

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜