|
|
|
@ -44,20 +44,20 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
|
|
|
|
|
private static final String TAG = "MainActivity";
|
|
|
|
|
|
|
|
|
|
private SpeechRecognizer mIat;// 语音听写对象
|
|
|
|
|
private RecognizerDialog mIatDialog;// 语音听写UI
|
|
|
|
|
private SpeechRecognizer mIat;// 语音听写对象
|
|
|
|
|
private RecognizerDialog mIatDialog;// 语音听写UI
|
|
|
|
|
|
|
|
|
|
// 用HashMap存储听写结果
|
|
|
|
|
// 用HashMap存储听写结果
|
|
|
|
|
private HashMap<String, String> mIatResults = new LinkedHashMap<String, String>();
|
|
|
|
|
|
|
|
|
|
private SharedPreferences mSharedPreferences;//缓存
|
|
|
|
|
private SharedPreferences mSharedPreferences;//缓存
|
|
|
|
|
|
|
|
|
|
private String mEngineType = SpeechConstant.TYPE_CLOUD;// 引擎类型
|
|
|
|
|
private String language = "zh_cn";//识别语言
|
|
|
|
|
private String mEngineType = SpeechConstant.TYPE_CLOUD;// 引擎类型
|
|
|
|
|
private String language = "zh_cn";//识别语言
|
|
|
|
|
|
|
|
|
|
private TextView tvResult;//识别结果
|
|
|
|
|
private Button btnStart;//开始识别
|
|
|
|
|
private String resultType = "json";//结果内容数据格式
|
|
|
|
|
private TextView tvResult;//识别结果
|
|
|
|
|
private Button btnStart;//开始识别
|
|
|
|
|
private String resultType = "json";//结果内容数据格式
|
|
|
|
|
private Button go;
|
|
|
|
|
private TextView tvResult1;
|
|
|
|
|
|
|
|
|
@ -71,12 +71,12 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
btnStart.setOnClickListener(this);
|
|
|
|
|
tvResult1=findViewById(R.id.tv_result1);
|
|
|
|
|
|
|
|
|
|
initPermission();//权限请求
|
|
|
|
|
initPermission();//权限请求
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 使用SpeechRecognizer对象,可根据回调消息自定义界面;
|
|
|
|
|
// 使用SpeechRecognizer对象,可根据回调消息自定义界面;
|
|
|
|
|
mIat = SpeechRecognizer.createRecognizer(yuyin.this, mInitListener);
|
|
|
|
|
// 使用UI听写功能,请根据sdk文件目录下的notice.txt,放置布局文件和图片资源
|
|
|
|
|
// 使用UI听写功能,请根据sdk文件目录下的notice.txt,放置布局文件和图片资源
|
|
|
|
|
mIatDialog = new RecognizerDialog(yuyin.this, mInitListener);
|
|
|
|
|
mSharedPreferences = getSharedPreferences("ASR",
|
|
|
|
|
Activity.MODE_PRIVATE);
|
|
|
|
@ -85,20 +85,20 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
if( null == mIat ){
|
|
|
|
|
// 创建单例失败,与 21001 错误为同样原因,参考 http://bbs.xfyun.cn/forum.php?mod=viewthread&tid=9688
|
|
|
|
|
showMsg( "创建对象失败,请确认 libmsc.so 放置正确,且有调用 createUtility 进行初始化" );
|
|
|
|
|
// 创建单例失败,与 21001 错误为同样原因,参考 http://bbs.xfyun.cn/forum.php?mod=viewthread&tid=9688
|
|
|
|
|
showMsg( "创建对象失败,请确认 libmsc.so 放置正确,且有调用 createUtility 进行初始化" );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mIatResults.clear();//清除数据
|
|
|
|
|
setParam(); // 设置参数
|
|
|
|
|
mIatDialog.setListener(mRecognizerDialogListener);//设置监听
|
|
|
|
|
mIatDialog.show();// 显示对话框
|
|
|
|
|
mIatResults.clear();//清除数据
|
|
|
|
|
setParam(); // 设置参数
|
|
|
|
|
mIatDialog.setListener(mRecognizerDialogListener);//设置监听
|
|
|
|
|
mIatDialog.show();// 显示对话框
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 初始化监听器。
|
|
|
|
|
* 初始化监听器。
|
|
|
|
|
*/
|
|
|
|
|
private InitListener mInitListener = new InitListener() {
|
|
|
|
|
|
|
|
|
@ -106,25 +106,25 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
public void onInit(int code) {
|
|
|
|
|
Log.d(TAG, "SpeechRecognizer init() code = " + code);
|
|
|
|
|
if (code != ErrorCode.SUCCESS) {
|
|
|
|
|
showMsg("初始化失败,错误码:" + code + ",请点击网址https://www.xfyun.cn/document/error-code查询解决方案");
|
|
|
|
|
showMsg("初始化失败,错误码:" + code + ",请点击网址https://www.xfyun.cn/document/error-code查询解决方案");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 听写UI监听器
|
|
|
|
|
* 听写UI监听器
|
|
|
|
|
*/
|
|
|
|
|
private RecognizerDialogListener mRecognizerDialogListener = new RecognizerDialogListener() {
|
|
|
|
|
public void onResult(RecognizerResult results, boolean isLast) {
|
|
|
|
|
|
|
|
|
|
printResult(results);//结果数据解析
|
|
|
|
|
printResult(results);//结果数据解析
|
|
|
|
|
printButton(results);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 识别回调错误.
|
|
|
|
|
* 识别回调错误.
|
|
|
|
|
*/
|
|
|
|
|
public void onError(SpeechError error) {
|
|
|
|
|
showMsg(error.getPlainDescription(true));
|
|
|
|
@ -133,7 +133,7 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 数据解析
|
|
|
|
|
* 数据解析
|
|
|
|
|
*
|
|
|
|
|
* @param results
|
|
|
|
|
*/
|
|
|
|
@ -141,7 +141,7 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
String text = JsonParser.parseIatResult(results.getResultString());
|
|
|
|
|
|
|
|
|
|
String sn = null;
|
|
|
|
|
// 读取json结果中的sn字段
|
|
|
|
|
// 读取json结果中的sn字段
|
|
|
|
|
try {
|
|
|
|
|
JSONObject resultJson = new JSONObject(results.getResultString());
|
|
|
|
|
sn = resultJson.optString("sn");
|
|
|
|
@ -156,14 +156,14 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
resultBuffer.append(mIatResults.get(key));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tvResult.setText(resultBuffer.toString());//听写结果显示
|
|
|
|
|
tvResult.setText(resultBuffer.toString());//听写结果显示
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
private void printButton(RecognizerResult results) {
|
|
|
|
|
String text = JsonParser.parseIatResult(results.getResultString());
|
|
|
|
|
|
|
|
|
|
String sn = null;
|
|
|
|
|
// 读取json结果中的sn字段
|
|
|
|
|
// 读取json结果中的sn字段
|
|
|
|
|
try {
|
|
|
|
|
JSONObject resultJson = new JSONObject(results.getResultString());
|
|
|
|
|
sn = resultJson.optString("sn");
|
|
|
|
@ -177,7 +177,7 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
for (String key : mIatResults.keySet()) {
|
|
|
|
|
resultBuffer.append(mIatResults.get(key));
|
|
|
|
|
}
|
|
|
|
|
if(resultBuffer.toString().equals("健康。")){
|
|
|
|
|
if(resultBuffer.toString().equals("健康。")){
|
|
|
|
|
Button button1 = findViewById(R.id.go);
|
|
|
|
|
tvResult1.setText(resultBuffer.toString());
|
|
|
|
|
|
|
|
|
@ -191,7 +191,7 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else if(resultBuffer.toString().equals("行程。")){
|
|
|
|
|
else if(resultBuffer.toString().equals("行程。")){
|
|
|
|
|
Button button1 = findViewById(R.id.go);
|
|
|
|
|
tvResult1.setText(resultBuffer.toString());
|
|
|
|
|
|
|
|
|
@ -205,7 +205,7 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else if(resultBuffer.toString().equals("核酸检测。")){
|
|
|
|
|
else if(resultBuffer.toString().equals("核酸检测。")){
|
|
|
|
|
Button button1 = findViewById(R.id.go);
|
|
|
|
|
tvResult1.setText(resultBuffer.toString());
|
|
|
|
|
|
|
|
|
@ -219,7 +219,7 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else if(resultBuffer.toString().equals("扫码付款。")){
|
|
|
|
|
else if(resultBuffer.toString().equals("扫码付款。")){
|
|
|
|
|
Button button1 = findViewById(R.id.go);
|
|
|
|
|
tvResult1.setText(resultBuffer.toString());
|
|
|
|
|
|
|
|
|
@ -233,7 +233,7 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else if(resultBuffer.toString().equals("付款码。")){
|
|
|
|
|
else if(resultBuffer.toString().equals("付款码。")){
|
|
|
|
|
Button button1 = findViewById(R.id.go);
|
|
|
|
|
tvResult1.setText(resultBuffer.toString());
|
|
|
|
|
|
|
|
|
@ -247,7 +247,7 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else if(resultBuffer.toString().equals("收款码。")){
|
|
|
|
|
else if(resultBuffer.toString().equals("收款码。")){
|
|
|
|
|
Button button1 = findViewById(R.id.go);
|
|
|
|
|
tvResult1.setText(resultBuffer.toString());
|
|
|
|
|
|
|
|
|
@ -261,7 +261,7 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else if(resultBuffer.toString().equals("设置常用联系人。")){
|
|
|
|
|
else if(resultBuffer.toString().equals("设置常用联系人。")){
|
|
|
|
|
Button button1 = findViewById(R.id.go);
|
|
|
|
|
tvResult1.setText(resultBuffer.toString());
|
|
|
|
|
|
|
|
|
@ -282,7 +282,7 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else if(resultBuffer.toString().equals("呼叫。")){
|
|
|
|
|
else if(resultBuffer.toString().equals("呼叫。")){
|
|
|
|
|
Button button1 = findViewById(R.id.go);
|
|
|
|
|
tvResult1.setText(resultBuffer.toString());
|
|
|
|
|
|
|
|
|
@ -297,7 +297,7 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else if(resultBuffer.toString().equals("一键通。")){
|
|
|
|
|
else if(resultBuffer.toString().equals("一键通。")){
|
|
|
|
|
Button button1 = findViewById(R.id.go);
|
|
|
|
|
tvResult1.setText(resultBuffer.toString());
|
|
|
|
|
|
|
|
|
@ -310,7 +310,7 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else if(resultBuffer.toString().equals("找寻。")){
|
|
|
|
|
else if(resultBuffer.toString().equals("找寻。")){
|
|
|
|
|
Button button1 = findViewById(R.id.go);
|
|
|
|
|
tvResult1.setText(resultBuffer.toString());
|
|
|
|
|
|
|
|
|
@ -323,7 +323,7 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else if(resultBuffer.toString().equals("健康。")){
|
|
|
|
|
else if(resultBuffer.toString().equals("健康。")){
|
|
|
|
|
Button button1 = findViewById(R.id.go);
|
|
|
|
|
tvResult1.setText(resultBuffer.toString());
|
|
|
|
|
|
|
|
|
@ -347,24 +347,24 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 参数设置
|
|
|
|
|
* 参数设置
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public void setParam() {
|
|
|
|
|
// 清空参数
|
|
|
|
|
// 清空参数
|
|
|
|
|
mIat.setParameter(SpeechConstant.PARAMS, null);
|
|
|
|
|
// 设置听写引擎
|
|
|
|
|
// 设置听写引擎
|
|
|
|
|
mIat.setParameter(SpeechConstant.ENGINE_TYPE, mEngineType);
|
|
|
|
|
// 设置返回结果格式
|
|
|
|
|
// 设置返回结果格式
|
|
|
|
|
mIat.setParameter(SpeechConstant.RESULT_TYPE, resultType);
|
|
|
|
|
|
|
|
|
|
if (language.equals("zh_cn")) {
|
|
|
|
|
String lag = mSharedPreferences.getString("iat_language_preference",
|
|
|
|
|
"mandarin");
|
|
|
|
|
Log.e(TAG, "language:" + language);// 设置语言
|
|
|
|
|
Log.e(TAG, "language:" + language);// 设置语言
|
|
|
|
|
mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
|
|
|
|
|
// 设置语言区域
|
|
|
|
|
// 设置语言区域
|
|
|
|
|
mIat.setParameter(SpeechConstant.ACCENT, lag);
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
@ -372,25 +372,25 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
}
|
|
|
|
|
Log.e(TAG, "last language:" + mIat.getParameter(SpeechConstant.LANGUAGE));
|
|
|
|
|
|
|
|
|
|
//此处用于设置dialog中不显示错误码信息
|
|
|
|
|
//此处用于设置dialog中不显示错误码信息
|
|
|
|
|
//mIat.setParameter("view_tips_plain","false");
|
|
|
|
|
|
|
|
|
|
// 设置语音前端点:静音超时时间,即用户多长时间不说话则当做超时处理
|
|
|
|
|
// 设置语音前端点:静音超时时间,即用户多长时间不说话则当做超时处理
|
|
|
|
|
mIat.setParameter(SpeechConstant.VAD_BOS, mSharedPreferences.getString("iat_vadbos_preference", "4000"));
|
|
|
|
|
|
|
|
|
|
// 设置语音后端点:后端点静音检测时间,即用户停止说话多长时间内即认为不再输入, 自动停止录音
|
|
|
|
|
// 设置语音后端点:后端点静音检测时间,即用户停止说话多长时间内即认为不再输入, 自动停止录音
|
|
|
|
|
mIat.setParameter(SpeechConstant.VAD_EOS, mSharedPreferences.getString("iat_vadeos_preference", "1000"));
|
|
|
|
|
|
|
|
|
|
// 设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点
|
|
|
|
|
// 设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点
|
|
|
|
|
mIat.setParameter(SpeechConstant.ASR_PTT, mSharedPreferences.getString("iat_punc_preference", "1"));
|
|
|
|
|
|
|
|
|
|
// 设置音频保存路径,保存音频格式支持pcm、wav,设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
|
|
|
|
|
// 设置音频保存路径,保存音频格式支持pcm、wav,设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
|
|
|
|
|
mIat.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
|
|
|
|
|
mIat.setParameter(SpeechConstant.ASR_AUDIO_PATH, Environment.getExternalStorageDirectory() + "/msc/iat.wav");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 提示消息
|
|
|
|
|
* 提示消息
|
|
|
|
|
* @param msg
|
|
|
|
|
*/
|
|
|
|
|
private void showMsg(String msg) {
|
|
|
|
@ -402,7 +402,7 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
|
|
|
|
|
if (null != mIat) {
|
|
|
|
|
// 退出时释放连接
|
|
|
|
|
// 退出时释放连接
|
|
|
|
|
mIat.cancel();
|
|
|
|
|
mIat.destroy();
|
|
|
|
|
}
|
|
|
|
@ -410,7 +410,7 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* android 6.0 以上需要动态申请权限
|
|
|
|
|
* android 6.0 以上需要动态申请权限
|
|
|
|
|
*/
|
|
|
|
|
private void initPermission() {
|
|
|
|
|
String permissions[] = {Manifest.permission.RECORD_AUDIO,
|
|
|
|
@ -434,7 +434,7 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 权限申请回调,可以作进一步处理
|
|
|
|
|
* 权限申请回调,可以作进一步处理
|
|
|
|
|
*
|
|
|
|
|
* @param requestCode
|
|
|
|
|
* @param permissions
|
|
|
|
@ -442,7 +442,7 @@ public class yuyin extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
|
|
|
|
// 此处为android 6.0以上动态授权的回调,用户自行实现。
|
|
|
|
|
// 此处为android 6.0以上动态授权的回调,用户自行实现。
|
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|