Speech recognition is convenient and easy to use on Android.
However, there is a premise that Google's voice search tool must be installed on Android machines in advance.
Speech recognition technology is added in Android sdk1.5 (recognizerintent). Here we will analyze the built-in API example, which is actually completed through an intent action. There are two main modes:
Action_recognize_speech: Speech Recognition. In this mode, we can capture the text column after speech processing.
Action_web_search: Network Search
This example uses the action_recognize_speech mode. We need to implement the onactivityresult method, which is the callback function after the speech recognition is complete.
The following is the core code:
Package com. Ifeisu. Test. Voice;
Import the Java. Util. ArrayList;
Import the Java. Util. List;
The import android. App. The Activity;
The import android. Content. Intent;
The import android. Content. PM. PackageManager;
The import android. Content. PM. ResolveInfo;
The import android. OS. Bundle;
The import android. Researched. RecognizerIntent;
The import android. View. The view;
The import android. View. The view. An OnClickListener;
The import android. Widget. ArrayAdapter;
The import android. Widget. The Button;
The import android. Widget. ListView.
/ * * *
* speech recognition demo, before using this demo, you need to confirm that Google's voice search tool is installed on your phone or emulator
* voice_search_2.1.4.apk or later
* @ the author at guoxinzz@163.com
* @ website http://www.ifeisu.com
*
* /
Public class VoiceRecognition extends the Activity implements an OnClickListener {
Private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
Private ListView mList;
/ * *
*
* Called with the activity is first created.
* /
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. OnCreate (savedInstanceState);
The setContentView (R.l ayout. Main);
Button speakButton = (Button) findViewById(r.id.tn_speak);
MList = (ListView) the findViewById (R.i d.l ist);
// Check to see if a recognition activity is present
PackageManager PM = getPackageManager ();
List<resolveinfo> activities = pm.queryintentactivities (new Intent(</resolveinfo>
RecognizerIntent. ACTION_RECOGNIZE_SPEECH), 0);
If (activities. The size ()! = 0)
{
SpeakButton. SetOnClickListener (this);
}
The else
{
SpeakButton. SetEnabled (false);
SpeakButton. SetText (" Recognizer not present ");
}
}
Public void onClick (View v)
{
If (v.g etId () = = R.i db tn_speak)
{
StartVoiceRecognitionActivity ();
}
}
Private void startVoiceRecognitionActivity ()
{
// delivery of speech recognition patterns via intents
Intent Intent = new Intent(recognizerint.action_recognize_speech);
// language patterns and free-form speech recognition
Intent. PutExtra (RecognizerIntent EXTRA_LANGUAGE_MODEL,
RecognizerIntent. LANGUAGE_MODEL_FREE_FORM);
// prompt voice to begin
Intent. PutExtra (RecognizerIntent EXTRA_PROMPT,
"Please name the website you want to open.");
// start performing our Intent, voice recognition
StartActivityForResult (intent, VOICE_RECOGNITION_REQUEST_CODE);
}
// the callback function onActivityResult when the voice ends
@ Override
Protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
If (requestCode = = VOICE_RECOGNITION_REQUEST_CODE
&& the resultCode = = RESULT_OK)
{
// gets the character of speech
ArrayList < String > matches = data
GetStringArrayListExtra (RecognizerIntent. EXTRA_RESULTS);
MList. SetAdapter (new ArrayAdapter < String > (this,
Android. R.l ayout simple_list_item_1, matches));
}
Super. OnActivityResult (requestCode, the resultCode, data);
}
}
1. If the mobile phone does not support voice search, you need to download Google's Voice Search toolkit voice_search_2.1.4.apk, which can be downloaded from hiapk.com or other app stores.
2. Download the source code: demo.voice_recognition.rar. Because csdn does not allow attachments to be uploaded, right-click the connection and select "connect to". Download and change the name to "demo.voice_recognition.rar.