// 录音对象
const recorderManager = wx.getRecorderManager();
/** 录音开始 */
function speechStart() {
const options = {
duration: 10000,
sampleRate: 16000,
numberOfChannels: 1,
encodeBitRate: 64000,
format: 'mp3',
frameSize: 50
}
recorderManager.start(options);
}
/** 语音识别 */
function speechRecognition( res) {
console.log("语音识别");
wx.uploadFile({
url: "http://bnwisdom.xicp.io/speechRecognition",
filePath: res.tempFilePath,
name: 'file',
formData: {
'user': 'test'
},
success: function (res) {
console.log(res);
// var data = JSON.parse(res);
console.log(res.data);
// msg 为最终语音识别的字符串
var msg = res.data;
// 获取当前页面对象
var page = getCurrentPages()[0];
page.setData({ msg: msg });
},
fail: function () {
console.log("语音识别失败");
page.setData({ msg: "语音识别失败,请重新录音" });
}
})
}
// 结束录音的时候触发
recorderManager.onStop((res) => {
// 获取文件路径-提交到后台-后台发送到百度
console.log("res.tempFilePath====" + res.tempFilePath);
speechRecognition(res);
})
recorderManager.onError((res) => {
console.log("error", res);
});
Page({
data: {
msg: "",
ldata: false,
errorNum: 0//无法匹配到设备的重试次数
},
// 按下按钮的时候触发
startrecorderHandel() {
speechStart()
},
// 松开按钮的时候触发-发送录音
sendrecorderHandel() {
// 结束录音
recorderManager.stop();
},