Android speech recognition technology Detailed and example code _android

Source: Internet
Author: User
Tags stub

Today from the Internet to find an example of the realization of speech recognition, personal feeling quite fun, put the code posted to share with you under:

Android mainly through the recognizerintent to achieve speech recognition, in fact, the code is relatively simple, but if the settings can not be found, will throw an exception activitynotfoundexception, so we need to catch this exception. and speech recognition can not be tested on the simulator, because speech recognition is to access the Google Cloud data, so if the mobile phone network is not open, you can not achieve the recognition of sound! Be sure to open the phone network, if the phone does not exist speech recognition function, it is also unable to enable recognition!

The following is the code in Recognizerintentactivity:

    
public class Recognizerintentactivity extends activity {private Button btnreconizer; 
  private static final int voice_recognition_request_code = 1234; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub Super.oncre 
    Ate (savedinstancestate); 
     
    Setcontentview (R.layout.reconizer); 
    Btnreconizer= (Button) This.findviewbyid (R.id.btnrecognizer); 
        Btnreconizer.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) { TODO auto-generated Method Stub try{//through Intent the mode of speech recognition, open the voice Intent intent=new Intent ( 
        Recognizerintent.action_recognize_speech); Language mode and Free mode speech recognition Intent.putextra (Recognizerintent.extra_language_model, recognizerintent.language_model_free_for 
        M); 
        Prompt speech starts Intent.putextra (Recognizerintent.extra_prompt, "Start speech"); Start speech recognition Startactivityforresult (intent, Voice_Recognition_request_code); 
          }catch (Exception e) {//Todo:handle Exception e.printstacktrace (); 
        Toast.maketext (Getapplicationcontext (), "Voice device not Found", 1). Show (); 
     
  } 
      } 
    }); @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {//TODO Auto-ge nerated method Stub//callback Gets the data obtained from Google if (Requestcode==voice_recognition_request_code && Resultcode==result _OK) {//Get phonetic character arraylist<string> Results=data.getstringarraylistextra (recognizerintent.extra_results) 
       
      ; 
      String resultstring= ""; 
      for (int i=0;i<results.size (); i++) {resultstring+=results.get (i); 
    } toast.maketext (This, resultstring, 1). Show (); 
  } super.onactivityresult (Requestcode, ResultCode, data); 

 } 
}

The main principle is to send voice to the Google Cloud, and then the cloud processing, matching the corresponding data, sent to the client.
Finally, don't forget to add network access to the manifest:

<uses-permission android:name= "Android.permission.INTERNET"/>

Post-run Effects:

Click the Start Voice button and start talking (here to make sure the phone's network is open):

Waiting for cloud data, because I this is 2G card, wait for a long time or not to load down, and so back to the company with the company's WiFi try, if the cloud data, it will be printed through the toast way.

The above is on the Android speech recognition technology data collation, follow-up continue to supplement the relevant information thank you for your support of this site!

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.