[TOC]

1.1. 使用麦克风当音听写示例

  1. //1.创建SpeechRecognizer对象
  2. SpeechRecognizer mIat= SpeechRecognizer.createRecognizer( );
  3. //2.设置听写参数,详见《MSC Reference Manual》SpeechConstant类
  4. mIat.setParameter(SpeechConstant.DOMAIN, "iat");
  5. mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
  6. mIat.setParameter(SpeechConstant.ACCENT, "mandarin ");
  7. //3.开始听写
  8. mIat.startListening(mRecoListener);
  9. //听写监听器
  10. private RecognizerListener mRecoListener = new RecognizerListener(){
  11. //听写结果回调接口(返回Json格式结果,用户可参见附录);
  12. //一般情况下会通过onResults接口多次返回结果,完整的识别内容是多次结果的累加;
  13. //关于解析Json的代码可参见MscDemo中JsonParser类;
  14. //isLast等于true时会话结束。
  15. public void onResult(RecognizerResult results, boolean isLast){
  16. DebugLog.Log("Result:"+results.getResultString ());
  17. }
  18. //会话发生错误回调接口
  19. public void onError(SpeechError error) {
  20. error.getPlainDescription(true) //获取错误码描述
  21. }
  22. //开始录音
  23. public void onBeginOfSpeech() {}
  24. //音量值0~30
  25. public void onVolumeChanged(int volume){}
  26. //结束录音
  27. public void onEndOfSpeech() {}
  28. //扩展用接口
  29. public void onEvent(int eventType,int arg1,int arg2,String msg) {}
  30. };

1.2. 使用音频文件听写示例

  1. //1.创建SpeechRecognizer对象
  2. SpeechRecognizer mIat= SpeechRecognizer.createRecognizer( );
  3. //2.设置听写参数,详见《MSC Reference Manual》SpeechConstant类
  4. mIat.setParameter(SpeechConstant.DOMAIN, "iat");
  5. mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
  6. mIat.setParameter (SpeechConstant.ACCENT, "mandarin ");
  7. mIat.setParameter(SpeechConstant.AUDIO_SOURCE, "-1");
  8. //3.开始听写
  9. mIat.startListening(mRecoListener);
  10. //voiceBuffer为音频数据流,splitBuffer为自定义分割接口,将其以4.8k字节分割成数组
  11. ArrayList<byte[]> buffers = splitBuffer(voiceBuffer,voiceBuffer.length, 4800);
  12. for (int i = 0; i < buffers.size(); i++) {
  13. // 每次写入msc数据4.8K,相当150ms录音数据
  14. mIat.writeAudio(buffers.get(i), 0, buffers.get(i).length);
  15. }
  16. mIat.stopListening();
  17. //听写监听器
  18. private RecognizerListener mRecoListener = new RecognizerListener(){
  19. public void onResult(RecognizerResult results, boolean isLast) {
  20. DebugLog.Log("Result:"+results.getResultString ());
  21. }
  22. //会话发生错误回调接口
  23. public void onError(SpeechError error) {
  24. error.getPlainDescription(true) //获取错误码描述
  25. }
  26. //开始录音
  27. public void onBeginOfSpeech() {}
  28. //音量值0~30
  29. public void onVolumeChange( int volume ) {}
  30. }

1.3. 上传用户词表

上传用户词表可以提高词表内词汇的识别率,也可以提高语义的效果,每个用户终端设备对应一个词表,用户词表的格式及构造方法详见《MSC Reference Manual》UserWords类。

  1. private void uploadUserWords() {
  2. SpeechRecognizer recognizer = SpeechRecognizer.getRecognizer();
  3. UserWords userwords = new UserWords(USER_WORDS);
  4. recognizer.setParameter( SpeechConstant.DATA_TYPE, "userword" );
  5. recognizer.updateLexicon("userwords", userwords.toString(), lexiconListener);
  6. }
  7. /**
  8. * 词表上传监听器
  9. */
  10. LexiconListener lexiconListener = new LexiconListener() {
  11. @Override
  12. public void onLexiconUpdated(String lexiconId, SpeechError error) {
  13. if (error == null)
  14. DebugLog.Log("*************上传成功*************");
  15. else
  16. DebugLog.Log("*************" + error.getErrorCode()+ "*************");
  17. }
  18. };