最近,项目中要用讯飞语音听写的功能,于是在官网学习了一下,并尝试做个小Demo,期间遇到了一些问题,列举一下:
(1)常报错误码11201,是未经授权的语音应用,意思是官方每天允许你使用的次数你已经用完了。
(2)常报错误码10407,用户校验失败,说明你的应用的Key与libs库不匹配,必须得用key去申请相应的libs.
(3)说话一次,返回两次结果,这是我们不想看到的,如下
解决办法就是将第二次停掉,如下代码:
//讯飞语音听写监听器中的方法,用来解析识别后的结果
private void printResult(RecognizerResult results) {
String text = JsonParser.parseIatResult(results.getResultString());
//防止重复监听,即说话一次,产生两次结果
mSpeechRecognizer.stopListening();
if(TextUtils.isEmpty(text)){
return;
}
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));
}
String res = resultBuffer.toString();
LogUtils.d(" TAG", "ccccccccc res===))>>" + res.toString());
tvShowAnswer.setSelected(true);
}
(4)还有一个常见问题是,你得到了听写的结果后,迅速被句号或者空白覆盖,我求助讯飞官方群没有解决掉,于是我只能用笨办法解决,在官方的Demo中专门将语音听写这一功能抽取出来,主要代码如下:
public class MainActivity extends Activity {
private static final String APPID="57709d8d";//"578c4896"; //讯飞语音ID
private static String TAG = MainActivity.class.getSimpleName();
private SpeechRecognizer mSpeechRecognizer;
//语音识别结果
private String mRecognizerResult;
private SharedPreferences mSharedPreferences;
// 用HashMap存储听写结果
private HashMap
private TextView tvShowInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
SpeechUtility.createUtility(this, SpeechConstant.APPID+"="+APPID);
setContentView(R.layout.activity_main);
// 使用SpeechRecognizer对象,可根据回调消息自定义界面;
mSpeechRecognizer = SpeechRecognizer.createRecognizer(MainActivity.this, mInitListener);
mSpeechRecognizer = SpeechRecognizer.createRecognizer(this, null);
mSharedPreferences = getSharedPreferences(IatSettings.PREFER_NAME,
Activity.MODE_PRIVATE);
tvShowInfo = (TextView) findViewById(R.id.tv_showinfo);
// 设置参数
mSpeechRecognizer.startListening(mRecognizerListener);
}
int ret=0;
public void doGet(View view) {
tvShowInfo.setText(null);
mIatResults.clear();
// 设置参数
setParam();
mSpeechRecognizer.startListening(mRecognizerListener);
if (ret != ErrorCode.SUCCESS) {
Log.d(TAG,"听写失败,错误码:" + ret);
} else {
Log.d(TAG,"vvvv===>"+getString(R.string.text_begin));
}
}
/**
* 初始化监听器。
*/
private InitListener mInitListener = new InitListener() {
@Override
public void onInit(int code) {
Log.d(TAG, "SpeechRecognizer init() code = " + code);
if (code != ErrorCode.SUCCESS) {
Log.d("TAG","xxx 初始化失败,错误码:" + code);
}
}
};
/**
* 语音识别监听器
*/
private RecognizerListener mRecognizerListener = new RecognizerListener() {
@Override
public void onVolumeChanged(int i, byte[] bytes) {
}
@Override
public void onBeginOfSpeech() {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onResult(RecognizerResult recognizerResult, boolean b) {
Log.d(TAG, "xxxx recognizerResult===> "+recognizerResult.getResultString());
printResult(recognizerResult);
if (b) {
// TODO 最后的结果
}
}
@Override
public void onError(SpeechError speechError) {
//mSpeechRecognizer.startListening(mRecognizerListener);
speechError.getPlainDescription(true);
Log.d("HHHHHH", "xxxxx error==>>>" + speechError);
}
@Override
public void onEvent(int i, int i1, int i2, Bundle bundle) {
}
};
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));
}
tvShowInfo.setText(resultBuffer.toString());
tvShowInfo.setSelected(true);
}
/**
* 初始化讯飞的服务 语音听写对象
*/
private void setParam(){
// 1.创建语音听写对象 第二个参数与服务方式关联 本地服务的话传初始化监听器,云服务的话传null
// if (mSpeechRecognizer == null) {
// mSpeechRecognizer = SpeechRecognizer.createRecognizer(MainActivity.this, null);
// }
//2.设置参数
// mSpeechRecognizer.setParameter(SpeechConstant.DOMAIN, "iat");
// mSpeechRecognizer.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
// mSpeechRecognizer.setParameter(SpeechConstant.ACCENT, "mandarin");
// // 设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点
// mSpeechRecognizer.setParameter(SpeechConstant.ASR_PTT, mSharedPreferences.getString("iat_punc_preference", "0"));
// 清空参数
mSpeechRecognizer.setParameter(SpeechConstant.PARAMS, null);
// 设置听写引擎
mSpeechRecognizer.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
// 设置返回结果格式
mSpeechRecognizer.setParameter(SpeechConstant.RESULT_TYPE, "json");
String lag = mSharedPreferences.getString("iat_language_preference",
"mandarin");
if (lag.equals("en_us")) {
// 设置语言
mSpeechRecognizer.setParameter(SpeechConstant.LANGUAGE, "en_us");
} else {
// 设置语言
mSpeechRecognizer.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
// 设置语言区域
mSpeechRecognizer.setParameter(SpeechConstant.ACCENT, lag);
}
// 设置语音前端点:静音超时时间,即用户多长时间不说话则当做超时处理
mSpeechRecognizer.setParameter(SpeechConstant.VAD_BOS, mSharedPreferences.getString("iat_vadbos_preference", "4000"));
// 设置语音后端点:后端点静音检测时间,即用户停止说话多长时间内即认为不再输入, 自动停止录音
mSpeechRecognizer.setParameter(SpeechConstant.VAD_EOS, mSharedPreferences.getString("iat_vadeos_preference", "1000"));
// 设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点
mSpeechRecognizer.setParameter(SpeechConstant.ASR_PTT, mSharedPreferences.getString("iat_punc_preference", "1"));
// 设置音频保存路径,保存音频格式支持pcm、wav,设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
// 注:AUDIO_FORMAT参数语记需要更新版本才能生效
mSpeechRecognizer.setParameter(SpeechConstant.AUDIO_FORMAT,"wav");
mSpeechRecognizer.setParameter(SpeechConstant.ASR_AUDIO_PATH, Environment.getExternalStorageDirectory()+"/msc/iat.wav");
}
@Override
protected void onDestroy() {
super.onDestroy();
mSpeechRecognizer.cancel();
mSpeechRecognizer.destroy();
}
}
希望能够帮助到你,完整代码:http://download.csdn.net/detail/ljt2724960661/9598314