Android Speech Recognition

Source: Internet
Author: User

Android Speech Recognition uses cloud technologies to recognize users' voice input, including voice control technologies. We will use the APIS provided by Google to implement this function. Feature: recognize the voice input by the user and print it on the list.

First, create the following activity and create a listview control under the button (The project identified by the back-and-forth explicit language ).

The Code is as follows:

Package COM. example. speechreg; import android. app. activity; import android. content. intent; import android. content. PM. packagemanager; // used to find the classes related to the local application software package import android. content. PM. resolveinfo; // import the information returned by the system intentfilter to Android. OS. bundle; import android. speech. recognizerintent; // audio recognition class import android. view. view; import android. view. view. onclicklistener; import android. widget. arrayadapter; // matching conversion class im stored in Arrays for the same data Port android. widget. button; import android. widget. listview; import Java. util. arraylist; import Java. util. list;/*** sample code that invokes the speech recognition intent API. */public class mainactivity extends activity implements onclicklistener {Private Static final int voice_recognition_request_code = 1234; // ID code required for the startactivityforresult operation private listview MList; @ override public void oncreate (Bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. voice_recognition); button speakbutton = (button) findviewbyid (R. id. btn_speak); MList = (listview) findviewbyid (R. id. listview1); packagemanager PM = getpackagemanager (); List <resolveinfo> activities = PM. queryintentactivities (new intent (recognizerintent. action_recognize_speech), 0); // you can find If (activities. Size ()! = 0) {speakbutton. setonclicklistener (this); // if this activity exists} else {speakbutton. setenabled (false); speakbutton. settext ("recognizer not present"); // otherwise, modify the button display value and set it to not optional} public void onclick (view V) {// If (v. GETID () = R. id. btn_speak) {startvoicerecognitionactivity () ;}} private void startvoicerecognitionactivity () {intent = new intent (recognizerintent. Action_recognize_speech); intent. putextra (recognizerintent. extra_language_model, recognizerintent. language_model_free_form); intent. putextra (recognizerintent. extra_prompt, "Speech Recognition Demo"); // you can specify startactivityforresult (intent, voice_recognition_request_code) for the intent call of speech recognition ); start an activity call that requires a return value} @ override protected void onactivityresult (INT requestcode, int resultcode, intent data) {// This function is neither an interface nor an abstract function. Why override? If (requestcode = Response & resultcode = result_ OK) {// fill the list view with the strings the recognizer thought it // cocould have heard arraylist <string> matches = data. getstringarraylistextra (recognizerintent. extra_results); // parses the storage to identify the returned result MList. setadapter (New arrayadapter <string> (this, android. r. layout. simple_list_item_1, matches); // display the result in listview} super. onactivityresult (requestcode, resultcode, data );}}

The running result is as follows. It automatically calls the Google Speech Recognition service activity and returns the execution result of a specific activity.

Http://blog.csdn.net/comkingfly/article/details/7359987 ()

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.