JAVA接收PCM文件使用百度语音进行识别

//设置APPID/AK/SK
public static final String APP_ID = " ";
public static final String API_KEY = " ";
public static final String SECRET_KEY = " ";

//接收pcm文件并对语音二进制数据进行识别
public void nb(@RequestParam(value=“file”) MultipartFile file) throws IOException, JSONException {
// 初始化一个AipSpeech
AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
byte[] bytes = file.getBytes();
JSONObject asrRes = client.asr(bytes, “pcm”, 16000, null);
System.out.println(asrRes);
if((“success.”).equals(asrRes.get(“err_msg”).toString())){
String result=asrRes.get(“result”).toString();
System.out.println(“语音识别出来的值是”+result);
} else {
if(Integer.parseInt(asrRes.get(“err_no”).toString())==3300) {
System.out.println(“输入参数不正确”);
} else if(Integer.parseInt(asrRes.get(“err_no”).toString())==3301) {
System.out.println(“音频质量过差”);
}else if(Integer.parseInt(asrRes.get(“err_no”).toString())==3308) {
System.out.println(“音频过长”);
}else if(Integer.parseInt(asrRes.get(“err_no”).toString())==3310) {
System.out.println(“音频文件过大”);
} else {
System.out.println(“语音识别失败!”);
}
}
}

猜你喜欢

转载自blog.csdn.net/weixin_43880874/article/details/88850989