package com.baidu.speech.recognizerdemo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import android.Manifest;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.speech.RecognitionListener;
import android.speech.SpeechRecognizer;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.baidu.speech.EventListener;
import com.baidu.speech.EventManager;
import com.baidu.speech.EventManagerFactory;
import com.baidu.speech.asr.SpeechConstant;
import com.baidu.speech.VoiceRecognitionService;
public class MainActivity{
public static final int STATUS_None = 0;
public static final int STATUS_WaitingReady = 2;
public static final int STATUS_Ready = 3;
public static final int STATUS_Speaking = 4;
public static final int STATUS_Recognition = 5;
private long speechEndTime = -1;
private long time;
private HashMap authInfo;
private int status = STATUS_None;
private boolean inited = false;
public MainActivity()
{
super();
System.out.println("MainActivity");
onCreate();
}
private static String DESC_TEXT = "精简版识别,带有SDK唤醒运行的最少代码,仅仅展示如何调用,\n" +
"也可以用来反馈测试SDK输入参数及输出回调。\n" +
"本示例需要自行根据文档填写参数,可以使用之前识别示例中的日志中的参数。\n" +
"需要完整版请参见之前的识别示例。\n" +
"需要测试离线命令词识别功能可以将本类中的enableOffline改成true,首次测试离线命令词请联网使用。之后请说出“打电话给张三”";
private EventManager asr;
private boolean logTime = true;
private boolean enableOffline = true; // 测试离线命令词,需要改成true
/**
* 测试参数填在这里
*/
private void start() {
System.out.println("start");
Map params = new LinkedHashMap();
String event = null;
event = SpeechConstant.ASR_START; // 替换成测试的event
params.put(SpeechConstant.ACCEPT_AUDIO_VOLUME, false);
params.put(SpeechConstant.VAD,SpeechConstant.VAD_DNN);
if (enableOffline){
params.put(SpeechConstant.DECODER, 2);//DECODER = 0 ,表示禁用离线功能,只使用在线功能;DECODER = 2 ,表示启用离线功能,但是SDK强制在线优先。
}
String json = null; //可以替换成自己的json
json = new JSONObject(params).toString(); // 这里可以替换成你需要测试的json
asr.send(event, json, null, 0, 0);
printLog("输入参数:" + json);
}
private void stop() {
asr.send(SpeechConstant.ASR_STOP, null, null, 0, 0); //
}
private void loadOfflineEngine() {
Map params = new LinkedHashMap();
params.put(SpeechConstant.DECODER, 2);
params.put(SpeechConstant.ASR_OFFLINE_ENGINE_GRAMMER_FILE_PATH, "assets://baidu_speech_grammar.bsg");
asr.send(SpeechConstant.ASR_KWS_LOAD_ENGINE, new JSONObject(params).toString(), null, 0, 0);
}
private void unloadOfflineEngine() {
asr.send(SpeechConstant.ASR_KWS_UNLOAD_ENGINE, null, null, 0, 0); //
}
// @Override
protected void onCreate() {
System.out.println("onCreate");
authInfo = new HashMap(3);
authInfo.put("appid", "10127572"); // 认证相关, key, 从开放平台(http://yuyin.baidu.com)中获取的key
authInfo.put("appkey", "rZh0DLQKcqMzYjNSf2mwVByA"); // 认证相关, key, 从开放平台(http://yuyin.baidu.com)中获取的key
authInfo.put("secret", "3137fd3bec47ff381c37505583a044c4"); // 认证相关, secret, 从开放平台(http://yuyin.baidu.com)secret
// authInfo.put("appid", "6536447"); // 认证相关, key, 从开放平台(http://yuyin.baidu.com)中获取的key
// authInfo.put("appkey", "iWwyLKKcNkXPeHc2sEpXwDuH"); // 认证相关, key, 从开放平台(http://yuyin.baidu.com)中获取的key
// authInfo.put("secret", "64c2eaebd8133edc289c8b543e84f40d"); // 认证相关, secret, 从开放平台(http://yuyin.baidu.com)secret
authInfo.put("dec-type", 1); // SDK的协议号, 0=分包协议, 1=流式协议。TODO 目前需要强制设置为0启动分包协议
authInfo.put("log_level", 6); // 打开日志, 不设置则为关闭
authInfo.put("decoder", 0); // 使用纯在线识别
authInfo.put("vad", "dnn"); // 开启基于dnn的语音活动检测模块
authInfo.put(SpeechConstant.PID, 15361); // 设置产品ID, 见 https://github.com/baidu/speech-samples/wiki/docs-android-v3 2.3输入事件一节的PID参数
// SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
// {
//
// String args = sp.getString("args", "");
// if (null != args) {
// print("参数集:" + args);
// intent.put("args", args);
// }
// }
SpeechRecognizer.createSpeechRecognizer(this, new ComponentName(this, VoiceRecognitionService.class));
initPermission();
asr = EventManagerFactory.create(this, "asr");
// 2) 注册输出事件的监听器
asr.registerListener(new com.baidu.speech.EventListener() {
String mAsrResult = "";
String mAsrTemp = ""; // 临时识别结果
@Override
public void onEvent(String name, String params, byte[] data, int offset, int length) {
if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_READY)) {
// 引擎就绪,可以说话,一般在收到此事件后通过UI通知用户可以说话了
status = STATUS_Ready;
System.out.println("have ready,you can speak");
}
if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_BEGIN)) {
// SDK已经检测到用户在说话
time = System.currentTimeMillis();
status = STATUS_Speaking;
// btn.setText("说完了");
System.out.println("check is speaking");
}
if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_VOLUME)) {
// 音量回调
}
if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_AUDIO)) {
// 音频回调
}
if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_END)) {
// SDK已经检测到用户在说话
speechEndTime = System.currentTimeMillis();
status = STATUS_Recognition;
System.out.println("check is speak end");
// btn.setText("识别中");
}
if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL)) {
// 临时识别结果, 长语音模式需要从此消息中取出结果
try {
final JSONObject json = new JSONObject(params);
System.out.println(name + ": " + json.toString(4));
final String result_type = json.getString("result_type");
if ("partial_result".equals(result_type)) { // 处理临时识别结果
final String best_result = json.getJSONArray("results_recognition").getString(0);
System.out.println("~asr result:" + best_result);
// txtResult.setText(best_result);
} else if ("nlu_result".equals(result_type)) { // 处理语义
String nlu = new String(data, offset, length);
System.out.println("~mean result: " + nlu);
} else if ("final_result".equals(result_type)) {
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_FINISH)) {
// 识别结束
try {
JSONObject json = new JSONObject(params);
int sub_error = json.optInt("sub_error");
// BugCat.cat(getApplicationContext(), sub_error); // 用辅助类BugCat进行错误原因分析
int error = json.optInt("error");
if (error == 0) {
long end2finish = System.currentTimeMillis() - speechEndTime;
status = STATUS_None;
// String best_result = json.getJSONArray(SpeechRecognizer.RESULTS_RECOGNITION).getString(0);
String best_result = "";
System.out.println("asr success:" + best_result);
String json_res = json.optString("origin_result");
try {
System.out.println("origin_result=\n" + new JSONObject(json_res).toString(4));
} catch (Exception e) {
System.out.println("origin_result=[warning: bad json]\n" + json_res);
}
// btn.setText("开始");
String strEnd2Finish = "";
if (end2finish < 60 * 1000) {
strEnd2Finish = "(waited " + end2finish + "ms)";
}
// txtResult.setText(best_result + strEnd2Finish);
time = 0;
} else {
time = 0;
status = STATUS_None;
StringBuilder sb = new StringBuilder();
switch (error) {
case SpeechRecognizer.ERROR_AUDIO:
sb.append("ERROR_AUDIO音频问题");
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
sb.append("ERROR_SPEECH_TIMEOUT没有语音输入");
break;
case SpeechRecognizer.ERROR_CLIENT:
sb.append("ERROR_CLIENT其它客户端错误");
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
sb.append("ERROR_INSUFFICIENT_PERMISSIONS权限不足");
break;
case SpeechRecognizer.ERROR_NETWORK:
sb.append("ERROR_NETWORK网络问题");
break;
case SpeechRecognizer.ERROR_NO_MATCH:
sb.append("ERROR_NO_MATCH没有匹配的识别结果");
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
sb.append("ERROR_RECOGNIZER_BUSY引擎忙");
break;
case SpeechRecognizer.ERROR_SERVER:
sb.append("ERROR_SERVER服务端错误");
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
sb.append("ERROR_NETWORK_TIMEOUT连接超时");
break;
default:
sb.append("other error其它错误, 请对照错误和错误描述");
break;
}
sb.append(":" + error);
System.out.println("asr fail:" + sb.toString());
// btn.setText("开始");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// ... 支持的输出事件和事件支持的事件参数见“输出事件”一节
}
});
asr.send(SpeechConstant.ASR_CANCEL, "{}", null, 0, 0);
asr.send(SpeechConstant.ASR_START, new JSONObject(authInfo).toString(), null, 0, 0);
printLog("输入参数:" + new JSONObject(authInfo).toString());
if (enableOffline) {
loadOfflineEngine(); //测试离线命令词请开启, 测试 ASR_OFFLINE_ENGINE_GRAMMER_FILE_PATH 参数时开启
}
}
public void startRecord()
{
System.out.println("startRecord");
start();
}
public void stopRecord()
{
System.out.println("stopRecord");
stop();
}
// @Override
// protected void onDestroy() {
// super.onDestroy();
// asr.send(SpeechConstant.ASR_CANCEL, "{}", null, 0, 0);
// if (enableOffline) {
// unloadOfflineEngine(); //测试离线命令词请开启, 测试 ASR_OFFLINE_ENGINE_GRAMMER_FILE_PATH 参数时开启
// }
// }
// EventListener 回调方法
// @Override
// public void onEvent(String name, String params, byte[] data, int offset, int length) {
// String logTxt = "name: " + name;
//
//
// if (params != null && !params.isEmpty()) {
// logTxt += " ;params :" + params;
// }
// if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL)) {
// if (params.contains("\"nlu_result\"")) {
// if (length > 0 && data.length > 0) {
// logTxt += ", 语义解析结果:" + new String(data, offset, length);
// }
// }
// } else if (data != null) {
// logTxt += " ;data length=" + data.length;
// }
// printLog(logTxt);
// }
private void printLog(String text) {
if (logTime) {
text += " ;time=" + System.currentTimeMillis();
}
text += "\n";
Log.i(getClass().getName(), text);
System.out.println(text + "\n");
}
/**
* android 6.0 以上需要动态申请权限
*/
private void initPermission() {
String permissions[] = {Manifest.permission.RECORD_AUDIO,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.INTERNET,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
ArrayList toApplyList = new ArrayList();
for (String perm :permissions){
// if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, perm)) {
// toApplyList.add(perm);
//进入到这里代表没有权限.
// }
}
String tmpList[] = new String[toApplyList.size()];
if (!toApplyList.isEmpty()){
// ActivityCompat.requestPermissions(this, toApplyList.toArray(tmpList), 123);
}
}
// @Override
// public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
// 此处为android 6.0以上动态授权的回调,用户自行实现。
// }
}
直接测试demo,不要修改任何代码