1. Register your account and add apps
2. Select App for Android platform, download SDK
3. Copy the Libs files from the SDK to the Libs directory of the Project
4. Add User Rights
<uses-permissionAndroid:name= "Android.permission.INTERNET"/> <uses-permissionAndroid:name= "Android.permission.RECORD_AUDIO"/> <uses-permissionAndroid:name= "Android.permission.ACCESS_NETWORK_STATE"/> <uses-permissionAndroid:name= "Android.permission.ACCESS_WIFI_STATE"/> <uses-permissionAndroid:name= "Android.permission.CHANGE_NETWORK_STATE"/> <uses-permissionAndroid:name= "Android.permission.READ_PHONE_STATE"/> <uses-permissionAndroid:name= "Android.permission.READ_CONTACTS"/> <uses-permissionAndroid:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permissionAndroid:name= "Android.permission.READ_EXTERNAL_STORAGE"/> <uses-permissionAndroid:name= "Android.permission.WRITE_SETTINGS"/> <uses-permissionAndroid:name= "Android.permission.CAMERA"/>
5. Initializing Voice Configuration objects
// The voice configuration object must be initialized so that the MSC voice service can be used Speechutility.createutility (This, speechconstant.appid+ "=59454d78");
6. Copy the resource files from the assets directory to the assets directory of the Android project
7. Identification code
//Create a Speech recognition dialog boxRecognizerdialog rd =NewRecognizerdialog ( This,NULL); //set parameters such as parameter Accent,languageRd.setparameter (Speechconstant.language, "ZH_CN");//EnglishRd.setparameter (speechconstant.accent, "Mandarin");//Mandarin//Setting the callback interfaceRd.setlistener (NewRecognizerdialoglistener () {@Override Public voidOnresult (Recognizerresult Recognizerresult,Booleanb) {//Get return resultsString result =recognizerresult.getresultstring (); LOG.E ("Result", result); LOG.E ("B", + + ""); } @Override Public voidonError (Speecherror speecherror) {}}); //Show dialog BoxRd.show ();
8. Because a JSON string is returned, it has to be parsed to use
result:{"SN": 2, "ls":true, "BG": 0, "Ed": 0, "ws": [{"BG": 0, "CW": [{"SC": 0.00, "W": "? "}]}]}
9. Parsing JSON strings
/*** Parse JSON String *@paramresult*/ protectedstring Parsejsondata (String json) {//using Gson to parse JSON stringsGson Gson =NewGson (); Voicebean Bean= Gson.fromjson (JSON, Voicebean.class); ArrayList<WS> WS =bean.ws; StringBuffer SB=NewStringBuffer (); for(WS ws2:ws) {String info= Ws2.cw.get (0). W; Sb.append (info); } returnsb.tostring (); }
10. Complete code
PackageCom.example.chat;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List;ImportCom.example.chat.VoiceBean.WS;ImportCom.google.gson.Gson;ImportCom.iflytek.cloud.RecognizerResult;Importcom.iflytek.cloud.SpeechConstant;ImportCom.iflytek.cloud.SpeechError;ImportCom.iflytek.cloud.SpeechSynthesizer;Importcom.iflytek.cloud.SpeechUtility;ImportCom.iflytek.cloud.ui.RecognizerDialog;ImportCom.iflytek.cloud.ui.RecognizerDialogListener;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.ListView; Public classMainactivityextendsActivity {StringBuffer strbuf; ListView MLV; PrivateList<talkbean>lists; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //The voice configuration object must be initialized so that the MSC voice service can be usedSpeechutility.createutility ( This, speechconstant.appid+ "=59454d78"); MLV=(ListView) Findviewbyid (r.id.lv); Lists=NewArraylist<talkbean>(); } Public voidClick (View view) {//Create a Speech recognition dialog boxRecognizerdialog rd =NewRecognizerdialog ( This,NULL); //set parameters such as parameter Accent,languageRd.setparameter (Speechconstant.language, "ZH_CN");//EnglishRd.setparameter (speechconstant.accent, "Mandarin");//MandarinStrbuf =NewStringBuffer (); //Setting the callback interfaceRd.setlistener (NewRecognizerdialoglistener () {@Override Public voidOnresult (Recognizerresult arg0,Booleanarg1) { //TODO auto-generated Method StubString result =arg0.getresultstring (); String Data=parsejsondata (Result); LOG.E ("Data", data); Strbuf.append (data); if(ARG1) {//end of answer.String Voice =strbuf.tostring (); LOG.E ("Result", voice); } LOG.E ("B", arg1+ ""); } @Override Public voidonError (Speecherror arg0) {//TODO auto-generated Method Stub } }); //Show dialog Boxrd.show (); } /*** Parse JSON String *@paramresult*/ protectedstring Parsejsondata (String json) {//using Gson to parse JSON stringsGson Gson =NewGson (); Voicebean Bean= Gson.fromjson (JSON, Voicebean.class); ArrayList<WS> WS =bean.ws; StringBuffer SB=NewStringBuffer (); for(WS ws2:ws) {String info= Ws2.cw.get (0). W; Sb.append (info); } returnsb.tostring (); }
11. Running results show
Android-based speech recognition