Android ApiDemos example resolution (46): App-& gt; Voice Recognition

Source: Internet
Author: User

In this example, you need to install an application that supports RecognizerIntent. ACTION_RECOGNIZE_SPEECH In the Android system, such as Google's Voice Search application.

 

 

The simulator is not installed by default. For details, see how to install APK on Android emulator to install a Voice Search on the simulator.

In this example, VoiceRecognition first checks whether RecognizerIntent. ACTION_RECOGNIZE_SPEECH is installed on the local machine through PackageManager. If yes, Enable Speak. Otherwise, "Recognizer not present" is displayed"

[Java]
// Check to see if a recognition activity is present
PackageManager pm = getPackageManager ();
List <ResolveInfo> activities = pm. queryIntentActivities (
New Intent (RecognizerIntent. ACTION_RECOGNIZE_SPEECH), 0 );
If (activities. size ()! = 0 ){
SpeakButton. setOnClickListener (this );
} Else {
SpeakButton. setEnabled (false );
SpeakButton. setText ("Recognizer not present ");
}
// Check to see if a recognition activity is present
PackageManager pm = getPackageManager ();
List <ResolveInfo> activities = pm. queryIntentActivities (
New Intent (RecognizerIntent. ACTION_RECOGNIZE_SPEECH), 0 );
If (activities. size ()! = 0 ){
SpeakButton. setOnClickListener (this );
} Else {
SpeakButton. setEnabled (false );
SpeakButton. setText ("Recognizer not present ");
}

 

If Google's Voice Search is installed on the machine, click "Speak !" The voice input dialog box is started:

 


The code for the Speak button is as follows:

[Java]
Intent 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 ");
StartActivityForResult (intent, VOICE_RECOGNITION_REQUEST_CODE );
Intent 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 ");
StartActivityForResult (intent, VOICE_RECOGNITION_REQUEST_CODE );

 

StartActivityForResult is used because we need to obtain user input from the voice input dialog box. A prompt is displayed in the RecognizerIntent. EXTRA_PROMPT definition box. RecognizerIntent. LANGUAGE_MODEL_FREE_FORM is the voice input type. The free format is used here, And WEB_SEARCH is mainly used for Web search.


The following code returns a response from the voice input dialog box:

[Java]
If (requestCode = VOICE_RECOGNITION_REQUEST_CODE
& 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 );
MList. setAdapter (new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_1,
Matches ));
}
If (requestCode = VOICE_RECOGNITION_REQUEST_CODE
& 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 );
MList. setAdapter (new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_1,
Matches ));
}

 

The text returned by speech recognition is displayed in the list:

 

Author: mapdigit

 

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.