Importjava.io.UnsupportedEncodingException;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.os.Environment;Importandroid.text.TextUtils;ImportAndroid.util.Log;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;ImportCom.iflytek.cloud.ErrorCode;ImportCom.iflytek.cloud.InitListener;ImportCom.iflytek.cloud.LexiconListener;ImportCom.iflytek.cloud.RecognizerListener;ImportCom.iflytek.cloud.RecognizerResult;Importcom.iflytek.cloud.SpeechConstant;ImportCom.iflytek.cloud.SpeechError;ImportCom.iflytek.cloud.SpeechListener;ImportCom.iflytek.cloud.SpeechRecognizer;Importcom.iflytek.cloud.SpeechUtility;ImportCom.iflytek.cloud.util.ContactManager.ContactListener;Importcom.iflytek.cloud.util.UserWords; Public classMainactivityextendsActivityImplementsOnclicklistener {Private StaticString TAG = "Iatdemo"; //Voice Dictation Objects PrivateSpeechRecognizer Miat; //dictate result content PrivateEditText Mresulttext; //User Glossary Download Results PrivateString Mdownloadresult = ""; PrivateToast Mtoast; Private intret; PrivateButton startrecording; PrivateString Voiceresult = ""; PrivateTextView Text_result; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Miat= Speechrecognizer.createrecognizer ( This, Minitlistener); Mtoast= Toast.maketext ( This, "", Toast.length_short); Text_result= (TextView) This. Findviewbyid (R.id.text_result); Startrecording= (Button) This. Findviewbyid (r.id.start_recording); Startrecording.setonclicklistener ( This); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } Public voidGetvoice () {speechutility.createutility (mainactivity. This, "APPID=540593C3"); //Setting ParametersSetParam (); //do not display Dictation dialog boxRET =miat.startlistening (Recognizerlistener); } Public voidSetParam () {miat.setparameter (Speechconstant.language,"ZH_CN"); //Set the voice front end pointMiat.setparameter (Speechconstant.vad_bos, "4000"); //Set the Voice post endpointMiat.setparameter (Speechconstant.vad_eos, "1000"); //Set PunctuationMiat.setparameter (SPEECHCONSTANT.ASR_PTT, "1"); //Set the audio save pathMiat.setparameter (Speechconstant.asr_audio_path, environment.getexternalstoragedirectory () + "/xunfei"); } /*** Initialize the listener. */ PrivateInitlistener Minitlistener =NewInitlistener () {@Override Public voidOnInit (intcode) {LOG.D (TAG,"SpeechRecognizer init () code =" +code); if(Code = =errorcode.success) {toast.maketext (mainactivity. This, "init success", 0). Show (); } } }; /*** User Glossary Download listener. */ PrivateSpeechlistener Downloadlistener =NewSpeechlistener () {@Override Public voidOnData (byte[] data) { Try { if(Data! =NULL&& data.length > 1) Mdownloadresult=NewString (data, "Utf-8"); } Catch(unsupportedencodingexception e) {e.printstacktrace (); }} @Override Public voidoncompleted (speecherror error) {if(Error! =NULL) { } Else if(Textutils.isempty (Mdownloadresult)) {}Else{Mresulttext.settext (""); Userwords Userwords=Newuserwords (mdownloadresult.tostring ()); if(Userwords = =NULL|| Userwords.getkeys () = =NULL) { return; } for(String Key:userwords.getKeys ()) {Mresulttext.append (key+ ":"); for(String word:userwords.getWords (key)) {Mresulttext.append (Word+ ","); } mresulttext.append ("\ n"); } }} @Override Public voidOnEvent (intEventType, Bundle params) { } }; /*** Dictation listener. */ PrivateRecognizerlistener Recognizerlistener =NewRecognizerlistener () {@Override Public voidOnbeginofspeech () {Toast.maketext (mainactivity. This, "Begin Speech", 0). Show (); } @Override Public voidonError (speecherror error) {Toast.maketext (mainactivity. This, Error.geterrordescription (), 0). Show (); } @Override Public voidOnendofspeech () {Toast.maketext (mainactivity. This, "End", 0). Show (); } @Override Public voidOnEvent (intEventType,intArg1,intarg2, String msg) {Toast.maketext (mainactivity). This, "on event", 0). Show (); } @Override Public voidOnresult (Recognizerresult results,Booleanislast) {String text=Jsonparser.parseiatresult (results.getresultstring ()); Voiceresult= Voiceresult +text; if(islast) {text_result.settext (Voiceresult); }} @Override Public voidOnvolumechanged (intvolume) { } }; @Overrideprotected voidOnresume () {//speechutility.getutility (). checkserviceinstalled (); Super. Onresume (); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.start_recording:getvoice (); Break; } } }
Android-demo speech recognition