Android Voice Search

Source: Internet
Author: User

Preface

The text search icon is added to the search box of the existing software to facilitate user input. We can also use this input method to improve user experience.
There are three methods for Speech Recognition: ① using intent to call the speech recognition program; ② using sevice; ③ using 3rd-party speech recognition library;
The following describes the implementation of the solution technology ① and ③.

1. Use intent to call the Speech Recognition Program

The Android system has provided us with the intent method to start, and only one "android. speech. action. the intent of RECOGNIZE_SPEECH can be called to the speech recognition software that has been installed in the system. we only need to obtain the returned value and display it. the call code is as follows:
Intent intent = new Intent (RecognizerIntent. ACTION_RECOGNIZE_SPEECH );
Intent. putExtra (RecognizerIntent. EXTRA_LANGUAGE_MODEL, RecognizerIntent. LANGUAGE_MODEL_FREE_FORM );
Intent. putExtra (RecognizerIntent. EXTRA_PROMPT, getString (R. string. hint_code ));
StartActivityForResult (intent, REQUEST_CODE_RECOGNITION_REQUEST );
 
Of course, if the user's machine does not install the relevant software, an error will be reported, so we still need to do one of the following.
1. Add exception capture and prompt for the above Code segment;
2. Check that the user's computer has installed the relevant software before the call. If no software is installed, the voice input button is hidden or not operable. The judgment code is as follows:
Public static boolean isApkInstalled (String strIntent ){
PackageManager packageManager = context. getPackageManager ();
List <ResolveInfo> activities = packageManager. queryIntentActivities (new Intent (strIntent), 0 );

Return activities! = Null &&! Activities. isEmpty ();
}
After the call, filter the value returned by the voice input in onActivityResult. for example, the result set data exists in intent. getStringArrayListExtra (RecognizerIntent. EXTRA_RESULTS); the following example shows the input results for the user to select the correct input value. Here is the pop-up dialog box.
If (requestCode = REQUEST_CODE_RECOGNITION_REQUEST & resultCode = RESULT_ OK ){
ArrayList <String> results = data. getStringArrayListExtra (RecognizerIntent. EXTRA_RESULTS );

If (results! = Null &&! Results. isEmpty ()){
Int size = results. size ();
AudioArray = new String [size];
Results. toArray (audioArray );

For (int I = 0; I <size; I ++ ){
String str = results. get (I );
If (str! = Null ){
Str. replaceAll ("","");
Str. replaceAll ("","");
}

AudioArray [I] = str;
}

Showdidio (DIALOG_AUDIO );
} Else {
Toast. makeText (this, R. string. hite_audio, Toast. LENGTH_SHORT). show ();
}
}

2. Access the 3rd-party speech recognition Library

The above method relies on the user's own language input installed, but if the user does not have a sub-machine, you can use a prompt to install the user. in fact, a better way is to connect the second and second sides of voice input to achieve more reliable. the following describes how to use the speech recognition library of Tencent.
The RecognizerDialog is mainly used in the Tencent voice recognition database. It inherits from the Dialog, so you only need to use showDialog () to directly display its input interface. The createDialog code is as follows:
RecognizerDialog recognizerDialog = new RecognizerDialog (MainActivity. this, "appid = 1234567 ");
// Appid should be written to appid recognizerDialog applied for from kickocom. setEngine ("sms", null, null); // "sms": normal text transcription recognizerDialog. setListener (new RecognizerDialogListener (){
@ Override
Public void onResults (ArrayList <RecognizerResult> results,
Boolean arg1 ){
StringBuffer result = new StringBuffer ();
For (RecognizerResult r: results ){
Result. append (r. text );
}
EditText. setText (result. toString ());
}
 
@ Override
Public void onEnd (SpeechError arg0 ){
 
}
});

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.