长语音识别-asrLong
返回内容介绍
public static String sample(TAipSpeech client) throws Exception {
//参数内容
String filePath ="G:/8k.pcm";//本地文件路径
byte[] audio = FileUtil.readFileByBytes(filePath);
int format = 1;//语音压缩格式编码 PCM-1 WAV-2 AMR-3 SILK-4
String callback_url = "http://www.xsshome.cn/txnotify";//用户回调url,需用户提供,用于平台向用户通知识别结果 需要是公网哦
String result = client.asrLong(filePath,format,callback_url );//长语音识别 本地文件路径
String result = client.asrLong(audio, format,callback_url );//长语音识别 本地文件byte数据
return result;
}
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xs.tencent.aai.bean.TXNotify;
import com.xs.tencent.aai.bean.TXResponse;
/**
* 腾讯AI 长语音识别回调方法
* @author 小帅丶
* 开发者们根据自己的实际情况进行修改哦。
* 注释掉的注解请自行引入根据自己的实际项目架构来。
* 鄙人是springmvc 本项目只做示例代码。就不引入springmvc相关的jar了
*/
//@Controller
public class AAINotifyController {
/**
* 长语音识别回调方法
* @param request
* @param response
* @throws IOException
*/
@RequestMapping(value="/txnotify",method={RequestMethod.POST,RequestMethod.GET},produces="text/html;charset=UTF-8")
@ResponseBody
public String TxNotify(HttpServletRequest request,HttpServletResponse response) throws Exception {
TXResponse txResponse = null;
InputStream inputStream;
String result = "";
String response_result = "";
response.setContentType("text/xml;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
try {
inputStream = request.getInputStream();
String s = null;
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
while ((s = in.readLine()) != null) {
result = result + s;
}
System.out.println("tx接收到的数据==="+result);
TXNotify notify = JSONObject.toJavaObject(JSON.parseObject(result), TXNotify.class);
if(notify.getRet()==0){
txResponse = new TXResponse();
txResponse.setMsg("ok");
txResponse.setRet(0);
result = JSONObject.toJSONString(txResponse);
System.out.println("返回的数据==="+result);
return result;
}else{
txResponse = new TXResponse();
txResponse.setMsg("fail");
txResponse.setRet(1);
result = JSONObject.toJSONString(txResponse);
System.out.println("返回的数据==="+result);
return result;
}
} catch (Exception e) {
System.out.println("====TX回调出错了"+e.getMessage()+"=========响应结果"+response_result);
txResponse = new TXResponse();
txResponse.setMsg("error");
txResponse.setRet(5);
result = JSONObject.toJSONString(txResponse);
System.out.println("返回的数据==="+result);
return result;
}
}
}
/**
* @author 小帅丶
* @类名称 TXNotify
* @remark 回调返回的数据对象
* @date 2018-1-15
*/
public class TXNotify {
public Data data;
//0表示成功,非0表示出错
public int ret;
//返回信息;ret非0时表示出错时错误原因
public String msg;
//省略set/get
public void setMsg(String msg) {
this.msg = msg;
}
public static class Data{
public String task_id;
public String text;
//省略set/get
}
}
/**
* @author 小帅丶
* @类名称 TXNotify
* @remark 给接口响应接收数据的对象
* @date 2018-1-15
*/
public class TXResponse {
//0表示成功,非0表示出错
public int ret;
//返回信息;ret非0时表示出错时错误原因
public String msg;
//省略set/get
}