Android科大讯飞语音识别应用
怎样在科大讯飞注册,创建应用就不说了,直接说怎么应用吧。下载sdk,将libs文件夹下的jar包,分别导入到项目中,如果使用科大讯飞使用带UI接口时,还需要将assets下文件拷贝到项目中;首先,在Application中进行初始化;SpeechUtility.createUtility(MyApplication.this,"appid=5875cd49");下面就说代码中的实际运用
·
怎样在科大讯飞注册,创建应用就不说了,直接说怎么应用吧。
下载sdk,将libs文件夹下的jar包,分别导入到项目中,如果使用科大讯飞使用带UI接口时,还需要将assets下文件拷贝到项目中;
首先,在Application中进行初始化,appid是在创建应用时科大讯飞给的id;
SpeechUtility.createUtility(MyApplication.this,"appid=5875cd49");
下面就说代码中的实际运用
//语音听写对象
private SpeechRecognizer mIat;
//语音听写ui
private RecognizerDialog mIatDialog;
private SharedPreferences mSharedPreferences;
private Toast mToast;
private String mEngineType=SpeechConstant.TYPE_CLOUD;
// 用HashMap存储听写结果
private HashMap<String, String> mIatResults = new LinkedHashMap<String, String>();
进行初始化
mIat=SpeechRecognizer.createRecognizer(MainActivity.this, null);
mIatDialog=new RecognizerDialog(MainActivity.this,null);
mSharedPreferences=getSharedPreferences(this.getPackageNam(), MODE_PRIVATE);
mToast=Toast.makeText(this, "", Toast.LENGTH_SHORT);
mEngineType=SpeechConstant.TYPE_CLOUD;
点击识别按钮后进行下列逻辑
mIatResults.clear();
//设置参数
setParam();
boolean isShowDialog=mSharedPreferences.getBoolean(getString(R.string.pref_key_iat_show), true);
if(isShowDialog){
mIatDialog.setListener(mRecognizerDialogListener);
mIatDialog.show();
showTip(getString(R.string.text_begin));
}else{
//因为我使用的是带ui的所以下面这个就没有做回掉监听
//不显示听写对话框
// ret=mIat.startListening(mRecognizerListener);
if(ret!=ErrorCode.SUCCESS){
showTip("听写失败"+ret);
}else{
showTip("开始听写"}
}
设置语音识别参数
* 设置参数
*/
private void setParam() {
//清空参数
mIat.setParameter(SpeechConstant.PARAMS,null);
//设置听写引擎
mIat.setParameter(SpeechConstant.ENGINE_TYPE,SpeechConstant.TYPE_CLOUD);
// 设置返回结果格式
mIat.setParameter(SpeechConstant.RESULT_TYPE, "json");
String lag = mSharedPreferences.getString("iat_language_preference",
"mandarin");
if (lag.equals("en_us")) {
// 设置语言
mIat.setParameter(SpeechConstant.LANGUAGE, "en_us");
} else {
// 设置语言
mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
// 设置语言区域
mIat.setParameter(SpeechConstant.ACCENT, lag);
}
// 设置语音前端点:静音超时时间,即用户多长时间不说话则当做超时处理
mIat.setParameter(SpeechConstant.VAD_BOS, mSharedPreferences.getString("iat_vadbos_preference", "4000"));
// 设置语音后端点:后端点静音检测时间,即用户停止说话多长时间内即认为不再输入, 自动停止录音
mIat.setParameter(SpeechConstant.VAD_EOS, mSharedPreferences.getString("iat_vadeos_preference", "1000"));
// 设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点
mIat.setParameter(SpeechConstant.ASR_PTT, mSharedPreferences.getString("iat_punc_preference", "0"));
// 设置音频保存路径,保存音频格式支持pcm、wav,设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
// 注:AUDIO_FORMAT参数语记需要更新版本才能生效
mIat.setParameter(SpeechConstant.AUDIO_FORMAT,"wav");
mIat.setParameter(SpeechConstant.ASR_AUDIO_PATH, Environment.getExternalStorageDirectory()+"/msc/iat.wav");
}
private void showTip(final String str) {
mToast.setText(str);
mToast.show();
}
UI监听器
/**
* 听写UI监听器
*/
private RecognizerDialogListener mRecognizerDialogListener = new RecognizerDialogListener() {
public void onResult(RecognizerResult results, boolean isLast) {
printResult(results);
}
/**
* 识别回调错误.
*/
public void onError(SpeechError error) {
showTip(error.getPlainDescription(true));
}
};
/**
* 解析数据
* @param results
*/
private void printResult(RecognizerResult results) {
String text = JsonParser.parseIatResult(results.getResultString());
String sn = null;
// 读取json结果中的sn字段
try {
JSONObject resultJson = new JSONObject(results.getResultString());
sn = resultJson.optString("sn");
} catch (JSONException e) {
e.printStackTrace();
}
mIatResults.put(sn, text);
StringBuffer resultBuffer = new StringBuffer();
for (String key : mIatResults.keySet()) {
resultBuffer.append(mIatResults.get(key));
}
//文本中显示识别后的文字
etNumber.setText(resultBuffer.toString());
etNumber.setSelection(etNumber.length());
}
基本的就这些了,没什么难得,只要理解了其中的代码就很容易
更多推荐
已为社区贡献1条内容
所有评论(0)