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