Android speech recognition

Source: Internet
Author: User

Android mainly through recognizerintent to achieve speech recognition, in fact, the code is relatively simple, but if you cannot find the settings, will throw an exception activitynotfoundexceptioN, so we need to catch this exception. and speech recognition is not tested on the simulator, because speech recognition is to access Google Cloud data, so if the phone's network is not turned on, you can not realize the recognition of sound! Be sure to turn on the phone's network, if the phone does not have speech recognition function, it is also unable to enable recognition!
Some constants of the recognizerintent:


We only need to pass an action and some properties through intent, and then start the voice with Startactivityforresult , the 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, "start voice, please speak .... "); Startactivityforresult (Intent, Voice_recognition_request_code);

First, no diagram is not programmed

below we go into Android speech recognition programming

1 . Create a simple layout activity_main.xml

<span style= "color: #000000;" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "Horizontal" >    <edittext        android:id= "@+id/edittext"        android:layout_width= "0DP"        android:layout_height= "wrap_content"        android:layout_weight= "3"        android:hint= "Enter search keywords"/>    <button         android:id= "@+id/btn_speech"         android:layout_width= "0DP"        android:layout_height= "Wrap_ Content "        android:text=" speech recognition "        android:layout_weight=" 1 "/>   </LinearLayout></span>

2, create an activity Mainactivity.java

Package Com.example.speechdetectortest;import Java.util.arraylist;import Android.app.activity;import Android.content.activitynotfoundexception;import Android.content.intent;import Android.net.Uri;import Android.os.bundle;import Android.speech.recognitionlistener;import Android.speech.recognizerintent;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.toast;public class Mainactivity extends Activity implements Onclicklistener{private Button mbtnspeech;private EditText medittex;private int voice_recognition_request_code = 10000 ; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); Mbtnspeech = (Button) Findviewbyid (R.id.btn_speech); Medittex = (EditText) Findviewbyid (R.id.edittext); Mbtnspeech.setonclicklistener (this);} @Overridepublic void OnClick (View v) {//TODO auto-generateD Method Stubswitch (V.getid ()) {case R.id.btn_speech:try {////The mode of passing speech recognition through Intent, turn on voice Intent Intent = new Intent (Recogniz Erintent.action_recognize_speech)///language mode and free form of speech recognition Intent.putextra (Recognizerintent.extra_language_model,  Recognizerintent.language_model_free_form); Prompt for voice start Intent.putextra (Recognizerintent.extra_prompt, "Start speech, please speak .....    "); Start speech recognition Startactivityforresult (intent, voice_recognition_request_code);} catch (Activitynotfoundexception e) {///Todo:handle exception//Cannot find a voice device device Toast.maketext (this, "cannot find a voice device device", Toast.len       Gth_long). Show (); StartActivity (New Intent ("Android.intent.action.VIEW", Uri.parse ("Market://search?q=pname: Com.google.android.voicesearch ")));} Break;default:break;}} When the voice ends the callback function Onactivityresult @Overrideprotected void onactivityresult (int requestcode, int resultcode, Intent data) {/  /TODO auto-generated method stubif (Requestcode = = Voice_recognition_request_code && ResultCode = = RESULT_OK) {// Get the voice character arraylist<string> Results = Data.getstringarraylistextra (recognizerintent.extra_results); if (RESULTS! = null) {//Set view update string strText = Results.get (0); Medittex.settext (StrText);}} Super.onactivityresult (Requestcode, ResultCode, data);}}
3, finally do not forget to add <uses-permission android:name= "Android.permission.INTERNET"/> permissions, because the need to connect to the network for speech recognition


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android speech recognition

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.