第一步:关于百度语音
1.配置 这个需要在百度注册账号 然后在官网获取key等等
Jar包
底层代码
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
权限
<service
android:name="com.baidu.speech.VoiceRecognitionService"
android:exported="false" />
服务
<meta-data
android:name="com.baidu.speech.APP_ID"
android:value="10308735" />
<meta-data
android:name="com.baidu.speech.API_KEY"
android:value="tgPciIeIhrHVBu6DiNn7YTdD" />
<meta-data
android:name="com.baidu.speech.SECRET_KEY"
android:value="c743fcb3b3d30ddb58b9bd0b816d4e25" />
自己申请的key和在官网管理包名
基本准备ok了
然后我们看看 核心类:
EventManager asr;
asr= EventManagerFactory.create(this,"asr");//初始化 传的asr是识别的意思 wp是唤醒官网上都有说明
asr.registerListener(this);//设置监听
然后写 启动start(){
asr.send(event, json, null, 0, 0);//event是想干嘛 比如启动是json是参数配置 通过SpeechConstant.ASR_START; 指令都在SpeechConstant类中 可以查看官网文档
Map<String, Object> params = new LinkedHashMap<String, Object>();
params.put(SpeechConstant.ACCEPT_AUDIO_VOLUME, false);json = new JSONObject(params).toString();
这样来添加 当然可以添加很多配置 具体配置内容都在官网文档
}
stop方法
private void stop() { asr.send(SpeechConstant.ASR_STOP, null, null, 0, 0); }destory方法@Override protected void onDestroy() { super.onDestroy(); asr.send(SpeechConstant.ASR_CANCEL, "{}", null, 0, 0); }接下来看看监听实现 implements EventListener这个监听
关于@Override public void onEvent(String s, String s1, byte[] bytes, int i, int i1) { if(s.equals(SpeechConstant.CALLBACK_EVENT_ASR_READY)){ // 引擎就绪,可以说话,一般在收到此事件后通过UI通知用户可以说话了 } if(s.equals(SpeechConstant.CALLBACK_EVENT_ASR_FINISH)){ // 识别结束. //下面是工具类 转换 如果报错不至于闪退 我们拦截一下 自定义 RecogResult recogResult1 = RecogResult.parseJson(s1); if (recogResult1.hasError()) { int errorCode = recogResult1.getError(); int subErrorCode = recogResult1.getSubError(); String error = "错误代码:" + errorCode + "\n错误信息:" + ErrorTranslation.recogError(errorCode)+ "\n具体内容:"+recogResult1.getDesc(); if (error != null) { bottomTextView.setText("出现错误请联系程序员"); stop(); } } } if (s.equals(SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL)) { RecogResult recogResult = RecogResult.parseJson(s1); //Log.d("Lin",recogResult+""); // 临时识别结果, 长语音模式需要从此消息中取出结果 String[] results = recogResult.getResultsRecognition(); //Log.d("Lin","result"+results+""); finalResult=results[0]; //这个finalResult 就是最后的识别结果啦 可以拿来用啦~ } }关于RecogResult类 :package com.example.admin.tim.voice; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /** * Created by fujiayi on 2017/6/24. */ public class RecogResult { private static final int ERROR_NONE = 0; private String origalJson; private String[] resultsRecognition; private String origalResult; private String sn; // 日志id, 请求有问题请提问带上sn private String desc; private String resultType; private int error = -1; private int subError = -1; public static RecogResult parseJson(String jsonStr) { RecogResult result = new RecogResult(); result.setOrigalJson(jsonStr); try { JSONObject json = new JSONObject(jsonStr); int error = json.optInt("error"); int subError = json.optInt("sub_error"); result.setError(error); result.setDesc(json.optString("desc")); result.setResultType(json.optString("result_type")); if (error == ERROR_NONE) { result.setOrigalResult(json.getString("origin_result")); JSONArray arr = json.optJSONArray("results_recognition"); if (arr!=null){ int size = arr.length(); String[] recogs = new String[size]; for (int i = 0; i < size; i++) { recogs[i] = arr.getString(i); } result.setResultsRecognition(recogs); } } } catch (JSONException e) { e.printStackTrace(); } return result; } public boolean hasError(){ return error != ERROR_NONE; } public boolean isFinalResult(){ return "final_result".equals(resultType); } public boolean isPartialResult() { return "partial_result".equals(resultType); } public boolean isNluResult(){ return "nlu_result".equals(resultType); } public String getOrigalJson() { return origalJson; } public void setOrigalJson(String origalJson) { this.origalJson = origalJson; } public String[] getResultsRecognition() { return resultsRecognition; } public void setResultsRecognition(String[] resultsRecognition) { this.resultsRecognition = resultsRecognition; } public String getSn() { return sn; } public void setSn(String sn) { this.sn = sn; } public int getError() { return error; } public void setError(int error) { this.error = error; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String getOrigalResult() { return origalResult; } public void setOrigalResult(String origalResult) { this.origalResult = origalResult; } public String getResultType() { return resultType; } public void setResultType(String resultType) { this.resultType = resultType; } public int getSubError() { return subError; } public void setSubError(int subError) { this.subError = subError; } }
ErrorTranslation类:
package com.example.admin.tim.Error; import android.speech.SpeechRecognizer; /** * Created by fujiayi on 2017/6/14. */ public class ErrorTranslation { public static String recogError(int errorCode){ String message; switch (errorCode) { case SpeechRecognizer.ERROR_AUDIO: message = "音频问题"; break; case SpeechRecognizer.ERROR_SPEECH_TIMEOUT: message = "没有语音输入"; break; case SpeechRecognizer.ERROR_CLIENT: message = "其它客户端错误"; break; case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS: message = "权限不足"; break; case SpeechRecognizer.ERROR_NETWORK: message = "网络问题"; break; case SpeechRecognizer.ERROR_NO_MATCH: message = "没有匹配的识别结果"; break; case SpeechRecognizer.ERROR_RECOGNIZER_BUSY: message = "引擎忙"; break; case SpeechRecognizer.ERROR_SERVER: message = "服务端错误"; break; case SpeechRecognizer.ERROR_NETWORK_TIMEOUT: message = "连接超时"; break; default: message = "未知错误:" + errorCode; break; } return message; } public static String WakeupError(int errorCode){ String message = null; switch (errorCode) { case 1: message = "参数错误"; break; case 2: message = "网络请求发生错误"; break; case 3: message = "服务器数据解析错误"; break; case 4: message = "网络不可用"; break; default: message = "未知错误:" + errorCode; break; } return message; } }