之前在钻研百度的实时语音辨认,并利用到了微信小程序中,写篇文章分享一下。
先看看实现的成果吧
前置条件
申请百度实时语音辨认 key 百度 AI 接入指南
创立小程序
设置小程序录音参数
在 index.js 中输出
const recorderManager = wx.getRecorderManager()
const recorderConfig = {
duration: 600000,
frameSize: 5, // 指定当录音大小达到 5KB 时触发 onFrameRecorded
format: 'PCM',
// 文档中没写这个参数也能够触发 onFrameRecorded 的回调,不过楼主亲测能够应用
sampleRate: 16000,
encodeBitRate: 96000,
numberOfChannels: 1
}
应用 websocket 连贯
linkSocket() {
let _this = this
// 这里的 sn 是百度实时语音用于排查日志,这里我图不便就用工夫戳了
let sn = new Date().getTime()
wx.showLoading({title: '辨认中...'})
recorderManager.start(recorderConfig)
// 开启链接
wx.connectSocket({
url: 'wss://vop.baidu.com/realtime_asr?sn=' + sn,
protocols: ['websocket'],
success() {console.log('连贯胜利')
_this.initEventHandle()}
})
},
// 监听 websocket 返回的数据
initEventHandle() {
let _this = this
wx.onSocketMessage((res) => {let result = JSON.parse(res.data.replace('\n',''))
if(result.type == 'MID_TEXT'){_this.tran(result.result, 'value')
_this.setData({
textDis: 'none',
value: result.result,
})
}
if(result.type == 'FIN_TEXT'){
let value = _this.data.text
let tranStr = value + result.result
_this.tran(tranStr, 'text')
_this.setData({
value: '',
valueEn: '',
textDis: 'block',
text: tranStr,
})
}
})
wx.onSocketOpen(() =>
// 发送数据帧
_this.wsStart()
console.log('WebSocket 连贯关上')
})
wx.onSocketError(function (res) {console.log('WebSocket 连贯关上失败')
})
wx.onSocketClose(function (res) {console.log('WebSocket 已敞开!')
})
},
发送开始、音频数据、完结帧
wsStart() {
let config = {
type: "START",
data: {
appid: XXXXXXXXX,// 百度实时语音辨认 appid
appkey: "XXXXXXXXXXXXXXXXXX",// 百度实时语音辨认 key
dev_pid: 15372,
cuid: "cuid-1",
format: "pcm",
sample: 16000
}
}
wx.sendSocketMessage({data:JSON.stringify(config),
success(res){console.log('发送开始帧胜利')
}
})
},
wsSend(data){
wx.sendSocketMessage({
data:data,
success(res){console.log('发送数据帧胜利')
}
})
},
wsStop(){
let _this = this
this.setData({click: true,})
_this.stop()
let config = {type: "FINISH"}
wx.hideLoading()
recorderManager.stop()
wx.sendSocketMessage({data:JSON.stringify(config),
success(res){console.log('发送完结帧胜利')
}
})
},
小程序录音回调
onShow: function () {
let _this = this
recorderManager.onFrameRecorded(function (res){
let data = res.frameBuffer
_this.wsSend(data)
})
recorderManager.onInterruptionBegin(function (res){console.log('录音中断')
_this.wsStopForAcc()})
recorderManager.onStop(function (res){console.log('录音进行')
})
},