Android Speech Recognition --- & gt; RecongnizerIntent implementation,

Source: Internet
Author: User

Android Speech Recognition ---> RecongnizerIntent implementation,

First, let's talk about the betting points:

Android uses RecognizerIntent to implement speech recognition. In fact, the code is relatively simple. However, if the setting cannot be found, an exception ActivityNotFoundException will be thrown, so we need to capture this exception. In addition, speech recognition cannot be tested on simulators. Because Speech Recognition accesses google cloud data, if the mobile phone's network is not enabled, it cannot recognize the sound! You must enable the network of the mobile phone. If the mobile phone does not have the speech recognition function, it cannot enable the recognition function. I use the speech software of moufei from HKUST, the software can be automatically called for speech recognition once it is run.

Some Summary of RecognizerIntent (which can be found in the Android API ):

Next we can check the usage of this class RecognizerIntent in Android, and then we start to use Intent to send messages.

1 try {2 // enable voice 3 Intent = new intent (RecognizerIntent. ACTION_RECOGNIZE_SPEECH); 4 // Speech Recognition in language mode and free mode 5 intent. putExtra (RecognizerIntent. EXTRA_LANGUAGE_MODEL, RecognizerIntent. LANGUAGE_MODEL_FREE_FORM); 6 // indicates that the voice starts 7 intent. putExtra (RecognizerIntent. EXTRA_PROMPT, "Start speech"); 8 // start Speech Recognition 9 startActivityForResult (intent, VOICE_RECOGNITION_REQUEST_CODE); 10} catch (Exception e) {11 // TODO: handle exception 12 e. printStackTrace (); 13 Toast. makeText (getApplicationContext (), "Voice device not found", 1 ). show (); 14}

Used as a module.

  • Mainly used first
RecognizerIntent. ACTION_RECOGNIZE_SPEECH enable voice
  • Then switch the voice mode and free mode
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
  • Then start speech recognition:
StartActivityForResult (intent, VOICE_RECOGNITION_REQUEST_CODE );
The main process is shown above. Naturally, the user's voice data needs to be called back.
1 @ Override 2 protected void onActivityResult (int requestCode, int resultCode, Intent data) {3 // TODO Auto-generated method stub 4 // call back to obtain data from google 5 if (requestCode = VOICE_RECOGNITION_REQUEST_CODE & resultCode = RESULT_ OK) {6 // obtain the character 7 ArrayList for voice input <String> results = data. getStringArrayListExtra (RecognizerIntent. EXTRA_RESULTS); 8 // obtain the required character 9 String resultString = ""; 10 for (int I = 0; I <results. size (); I ++) {11 resultString + = results. get (I); 12} 13 Toast. makeText (this, resultString, 1 ). show (); 14} 15 super. onActivityResult (requestCode, resultCode, data); 16}

This combination can achieve speech recognition.

 
 


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.