Use of the built-in voice module in android Development

Source: Internet
Author: User

Requirement: Add a Voice Search module to the project to improve user experience.
Solution: Search for Voice Search examples on the Internet and add your own project to complete the product requirements by referring to the online code. This problem is well solved. You can find a lot of information on the Internet, but you can use it without directly importing the project. I wrote a complete Demo here, the code can be directly pasted into your project to implement voice search and display the search results.
General process of Voice Search: Start the built-in Intent, set the Intent parameter to RecognizerIntent. ACTION_RECOGNIZE_SPEECH, and add some prompt parameters.
[Java]
Intent. putExtra (RecognizerIntent. EXTRA_LANGUAGE_MODEL, RecognizerIntent. LANGUAGE_MODEL_FREE_FORM );
Intent. putExtra (RecognizerIntent. EXTRA_PROMPT, "Start talking ");

After completing the voice search, I receive data in onActivityResult. Here I only show the first result of the voice search. The system provides many results, which are arranged based on similarity. I used handler in the Demo and showed the data in handler. In this case, some data is processed in handler. Some speech search crashes during the development process, and it is finally found that do not perform time-consuming operations during result display.
The Code is as follows:
[Java]
Public class MainActivity extends Activity {
Private Button btnVoice;
Private TextView tvVoiceResult;
Private static final int voice_recognition_request_codes = 123456;

Private Handler jumpHandler = new Handler (){
Public void handleMessage (android. OS. Message msg ){
TvVoiceResult. setText (String) msg. obj );
};
};

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
BtnVoice = (Button) findViewById (R. id. btnVoice );
TvVoiceResult = (TextView) findViewById (R. id. tvVoiceResult );
BtnVoice. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
Try {
Intent intent = new Intent (RecognizerIntent. ACTION_RECOGNIZE_SPEECH );
Intent. putExtra (RecognizerIntent. EXTRA_LANGUAGE_MODEL, RecognizerIntent. LANGUAGE_MODEL_FREE_FORM );
Intent. putExtra (RecognizerIntent. EXTRA_PROMPT, "Start talking ");
StartActivityForResult (intent, VOICE_RECOGNITION_REQUEST_CODE );
} Catch (ActivityNotFoundException e ){
AlertDialog. Builder builder = new AlertDialog. Builder (MainActivity. this );
Builder. setTitle ("Speech Recognition ");
Builder. setMessage ("your mobile phone does not currently support the Voice Search function. Click" OK "to download and install the Google Voice Search software. You can also search for "Voice Search" in each app store to download and install it. ");
Builder. setPositiveButton ("OK", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// Jump to the downloaded voice page
}
});
Builder. setNegativeButton ("cancel", null );
Builder. show ();
}
}
});
}
 
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. activity_main, menu );
Return true;
}
 
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
If (requestCode = VOICE_RECOGNITION_REQUEST_CODE & resultCode = MainActivity. RESULT_ OK ){
ArrayList <String> matches = data. getStringArrayListExtra (RecognizerIntent. EXTRA_RESULTS );
If (matches. size ()> 0 ){
Message msg = new Message ();
Msg. obj = matches. get (0 );
JumpHandler. sendMessage (msg );
}
}
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.