Use of the system's own voice module in Android development

Source: Internet
Author: User

Use of the system's own voice module in Android development
Requirements: Add a voice search module to the project to increase the user experience
Solve the process: Search the Internet for examples of voice searches, refer to the online code, to join their own projects to complete the product requirements. This problem is very good to solve, online can find a lot of information, but not directly into the project can be used by the example, I wrote a complete demo here, the code can be pasted directly into their projects to achieve a voice search, and display the results of the search.
Voice search Approximate process: Boot system comes with the Intent,intent parameter set to Recognizerintent.action_recognize_speech, plus some hint parameters
[Java]
Intent.putextra (Recognizerintent.extra_language_model, recognizerintent.language_model_free_form);
Intent.putextra (recognizerintent.extra_prompt, "please start talking");

After the completion of the voice search in the Onactivityresult data reception, I just show the first result of voice search, the system provides a lot of results, is based on the similarity of the arrangement. I used the handler in the demo to show the data in the handler. In this case in the project, some processing of the data is done in handler. In the process of development encountered some of the phenomenon of speech search crashes, and finally found that in the results show do not do some time-consuming operation.
The code is as follows
[Java]
public class Mainactivity extends Activity {
Private Button Btnvoice;
Private TextView Tvvoiceresult;
private static final int voice_recognition_request_code = 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, "please 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 phone does not support voice search, click OK to download and install Google Voice search software.") You can also search for a "voice search" in each App Store to download and install. ");
Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Jump to the Download 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);
}
}
}

}

Use of the system's own voice module in Android development

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.