Android語音轉文字一識別語音
阿新 • • 發佈:2018-10-31
訊飛AndroidSDK文件:https://doc.xfyun.cn/msc_android/%E9%A2%84%E5%A4%87%E5%B7%A5%E4%BD%9C.html
訊飛語音SDK不支援android平臺語音轉寫的功能(只支援java平臺,當然android可以通過訪問後臺去請求),這裡是通過語音聽寫來識別音訊檔案內容,支援音訊長度≤60s(沒有測試過音訊檔案這麼長的)。
整合訊飛語音SDK到專案後,可以建立一個識別類,將下面程式碼複製到專案即可
建立SpeechRecognizer例項,一些初始化操作
@Override public void start(Context context) { mRecognizer = SpeechRecognizer.createRecognizer(mContext, mInitListener); mSpeechResult = new StringBuilder(); if (mRecognizer == null) { return; } mUserList = UserManager.getUserList(); UserWords userWords = new UserWords(); //上傳熱詞,比如用名,關鍵字,可提高識別準確度 for (User user : mUserList) { userWords.putWord(user.getName()); } mRecognizer.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); mRecognizer.setParameter(SpeechConstant.TEXT_ENCODING, "utf-8"); ret = mRecognizer.updateLexicon("userword", userWords.toString(), new LexiconListener() { @Override public void onLexiconUpdated(String s, SpeechError error) { if (error != null) { LogUtil.e(error.toString()); } else { LogUtil.e("success"); } } }); if (ret != ErrorCode.SUCCESS) mView.showTip("上傳熱詞失敗,錯誤碼:" + ret); }
設定識別引數:
//設定引數 public void setParam(String vadEos, String asrPtt, String asrPath) { if (mRecognizer == null) { return; } mView.stopPlayer(); mRecognizer.setParameter(SpeechConstant.PARAMS, null); // 設定引擎 mRecognizer.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); // 設定返回結果格式 mRecognizer.setParameter(SpeechConstant.RESULT_TYPE, "json"); mRecognizer.setParameter(SpeechConstant.LANGUAGE, "zh_cn"); // 設定語言區域 mRecognizer.setParameter(SpeechConstant.ACCENT, "zh_cn"); // 設定語音前端點:靜音超時時間,即使用者多長時間不說話則當做超時處理 mRecognizer.setParameter(SpeechConstant.VAD_BOS, "4000"); // 設定語音後端點:後端點靜音檢測時間,即使用者停止說話多長時間內即認為不再輸入, 自動停止錄音 mRecognizer.setParameter(SpeechConstant.VAD_EOS, vadEos); // 設定標點符號,設定為"0"返回結果無標點,設定為"1"返回結果有標點 mRecognizer.setParameter(SpeechConstant.ASR_PTT, asrPtt); mRecognizer.setParameter(SpeechConstant.AUDIO_FORMAT, "wav"); //是否儲存識別語音檔案 mRecognizer.setParameter(SpeechConstant.ASR_AUDIO_PATH, asrPath); //設定音訊資源,通過寫音訊流方式(-1),如果通過Android自帶的錄音機錄製音訊方式(可以不用設定) mRecognizer.setParameter(SpeechConstant.AUDIO_SOURCE, "-1"); } //設定引數完成後,開始識別 public void startRecognizer(File asrFile) { ret = mRecognizer.startListening(mRecognizerListener); if (ret != ErrorCode.SUCCESS) { mView.showTip("聽寫失敗,錯誤碼:" + ret); return; } if (asrFile == null) { return; } final byte[] audioData = FileUtils.readFile(asrFile); if (audioData != null) { mRecognizer.writeAudio(audioData, 0, audioData.length); mRecognizer.stopListening(); } else { mRecognizer.cancel(); LogUtil.e("read audiorecord file failed!"); } }
監聽識別結果:
private RecognizerListener mRecognizerListener = new RecognizerListener() { @Override public void onBeginOfSpeech() { mView.showTip("開始說話"); mSpeechResult.delete(0, mSpeechResult.length()); } @Override public void onEndOfSpeech() { mView.showTip("結束說話"); } @Override public void onResult(RecognizerResult results, boolean isLast) { final String result = JsonParser.parseIatResult(results.getResultString()); mSpeechResult.append(result); //可在這列印結果 mSpeechResult.toString(); } @Override public void onError(SpeechError error) { mView.showTip(error.getPlainDescription(true)); } @Override public void onVolumeChanged(int volume, byte[] data) { mView.showTip("當前正在說話,音量大小:" + volume); } @Override public void onEvent(int eventType, int arg1, int arg2, Bundle obj) { } };
FileUtils
public static byte[] readFile(File file) {
FileInputStream fis = null;
byte[] bytes = null;
try {
fis = new FileInputStream(file);
bytes = new byte[fis.available()];
fis.read(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bytes;
}