package com.iflytek.test;
public class TestMscRecognition {
private static String fileName = "G:/留言測(cè)試wav/201712121458598267.wav";
// private static String fileName = "E:/amr文件/11.wav";
public static void main(String[] args) {
MscRecognition mObject = new MscRecognition();
String result = null;
result = mObject.voiceTowords(fileName);
if (result.length() > 0) {
System.out.println("識(shí)別的文字:" + result);
} else {
System.out.println("空語(yǔ)音");
}
}
}
package com.iflytek.test;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.util.ArrayList;import com.iflytek.cloud.speech.RecognizerListener;import com.iflytek.cloud.speech.RecognizerResult;import com.iflytek.cloud.speech.Setting;import com.iflytek.cloud.speech.SpeechConstant;import com.iflytek.cloud.speech.SpeechError;import com.iflytek.cloud.speech.SpeechRecognizer;import com.iflytek.cloud.speech.SpeechUtility;/** * 音頻文件識(shí)別 * @author zxh * */public class MscRecognition {// 賬號(hào)IDprivate static final String APPID = "5a290e4a";private StringBuffer mResult = new StringBuffer();/** 最大等待時(shí)間, 單位ms */private int maxWaitTime = 500;/** 每次等待時(shí)間 */private int perWaitTime = 100;/** 音頻文件 */private String fileName = "";static {Setting.setShowLog(false);SpeechUtility.createUtility("appid=" + APPID);}public String voiceTowords(String fileName) {return initVoiceTowords(fileName, true);}/** * * @param fileName * @param init * 初始化最大等待時(shí)間 * @return */public String initVoiceTowords(String fileName, boolean init) {if (init) {maxWaitTime = 6000;}this.fileName = fileName;return recognize();}// *************************************音頻流聽(tīng)寫(xiě)*************************************/** * 聽(tīng)寫(xiě) * * @return */private String recognize() {if (SpeechRecognizer.getRecognizer() == null)SpeechRecognizer.createRecognizer();return RecognizePcmfileByte();}/** * 自動(dòng)化測(cè)試注意要點(diǎn) 如果直接從音頻文件識(shí)別,需要模擬真實(shí)的音速,防止音頻隊(duì)列的堵塞 * */private String RecognizePcmfileByte() {// 1、讀取音頻文件FileInputStream fis = null;byte[] voiceBuffer = null;try {fis = new FileInputStream(new File(fileName));voiceBuffer = new byte[fis.available()];fis.read(voiceBuffer);} catch (Exception e) {e.printStackTrace();} finally {try {if (null != fis) {fis.close();fis = null;}} catch (IOException e) {e.printStackTrace();}}// 2、音頻流聽(tīng)寫(xiě)if (0 == voiceBuffer.length) {mResult.append("no audio avaible!");} else {// 解析之前將存出結(jié)果置為空mResult.setLength(0);SpeechRecognizer recognizer = SpeechRecognizer.getRecognizer();// 設(shè)置參數(shù),詳見(jiàn)《MSC Reference Manual》SpeechConstant類(lèi)// 應(yīng)用領(lǐng)域用于聽(tīng)寫(xiě)和語(yǔ)音語(yǔ)義服務(wù)recognizer.setParameter(SpeechConstant.DOMAIN, "iat");// 語(yǔ)言區(qū)域recognizer.setParameter(SpeechConstant.LANGUAGE, "zh_cn");// 方言 每一種語(yǔ)言區(qū)域recognizer.setParameter(SpeechConstant.ACCENT, "mandarin");// 音頻源recognizer.setParameter(SpeechConstant.AUDIO_SOURCE, "-1");// 音頻采樣率recognizer.setParameter(SpeechConstant.SAMPLE_RATE, "8000");// 返回格式recognizer.setParameter(SpeechConstant.RESULT_TYPE, "plain");// 開(kāi)始聽(tīng)寫(xiě)recognizer.startListening(recListener);// voiceBuffer為音頻數(shù)據(jù)流,splitBuffer為自定義分割接口,將其以4.8k字節(jié)分割成數(shù)組ArrayListbuffers = splitBuffer(voiceBuffer, voiceBuffer.length, 4800);for (int i = 0; i < buffers.size(); i++) {// 每次寫(xiě)入msc數(shù)據(jù)4.8K,相當(dāng)150ms錄音數(shù)據(jù)recognizer.writeAudio(buffers.get(i), 0, buffers.get(i).length);}recognizer.stopListening();System.out.println(recognizer.isListening());// 在原有的代碼基礎(chǔ)上主要添加了這個(gè)while代碼等待音頻解析完成,recognizer.isListening()返回true,說(shuō)明解析工作還在進(jìn)行while (recognizer.isListening()) {if (maxWaitTime < 0) {mResult.setLength(0);mResult.append("解析超時(shí)!");break;}try {Thread.sleep(perWaitTime);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}maxWaitTime -= perWaitTime;}}return mResult.toString();}/** * 將字節(jié)緩沖區(qū)按照固定大小進(jìn)行分割成數(shù)組 * * @param buffer * 緩沖區(qū) * @param length * 緩沖區(qū)大小 * @param spsize * 切割塊大小 * @return */private ArrayListsplitBuffer(byte[] buffer, int length, int spsize) {ArrayListarray = new ArrayList();
if (spsize <= 0 || length <= 0 || buffer == null || buffer.length < length)
return array;
int size = 0;
while (size < length) {
int left = length - size;
if (spsize < left) {
byte[] sdata = new byte[spsize];
System.arraycopy(buffer, size, sdata, 0, spsize);
array.add(sdata);
size += spsize;
} else {
byte[] sdata = new byte[left];
System.arraycopy(buffer, size, sdata, 0, left);
array.add(sdata);
size += left;
}
}
return array;
}
/**
* 聽(tīng)寫(xiě)監(jiān)聽(tīng)器
*/
private RecognizerListener recListener = new RecognizerListener() {
// 開(kāi)始錄音
public void onBeginOfSpeech() {
}
// 結(jié)束錄音
public void onEndOfSpeech() {
}
// 音量值0~30
public void onVolumeChanged(int volume) {
}
public void onResult(RecognizerResult result, boolean islast) {
mResult.append(result.getResultString());
}
// 會(huì)話發(fā)生錯(cuò)誤回調(diào)接口
public void onError(SpeechError error) {
System.out.println("---------------error");
error.getErrorDescription(true);// 獲取錯(cuò)誤碼描述
System.out.println("錯(cuò)誤碼:" + error.getErrorCode());
}
// 擴(kuò)展用接口
public void onEvent(int eventType, int arg1, int agr2, String msg) {
}
};
}