//开始录音的时候
start: function () {
this.setData({
sayimg: ‘’,//图片样式
anmationShow: true,
})
//开始录音
wx.showLoading({
title: ‘录音中’,
mask: true
})
recorderManager.start(options);
recorderManager.onStart(() => {
console.log(‘recorder start’)
});
//错误回调
recorderManager.onError((res) => {
console.log(res);
})
recorderManager.onFrameRecorded((res) => {
const { frameBuffer } = res
console.log(‘frameBuffer’, frameBuffer)
sendSocketMessage(frameBuffer)
})
let socketOpen = false
const socketMsgQueue = []
wx.connectSocket({
url: ‘wss://地址’
})
wx.onSocketOpen(function (res) {
socketOpen = true
})
function sendSocketMessage(msg) {
console.log(msg)
if (socketOpen) {
wx.sendSocketMessage({
data: msg
})
} else {
socketMsgQueue.push(msg)
}
console.log(msg)
}
wx.onSocketError(function(res){
console.log(‘WebSocket连接打开失败’)
})
wx.onSocketMessage(function (res) {
console.log(‘收到服务器内容’ + JSON.stringify(res))
})
},
//停止录音
stop: function () {
wx.hideLoading();
this.setData({
sayimg: ‘’,
anmationShow: false,
})
var that = this;
recorderManager.stop();
recorderManager.onStop((res) => {
that.tempFilePath = res.tempFilePath;
console.log(‘recorder end’)
console.log(‘停止录音’, res.tempFilePath)
const { tempFilePath } = res;
})
},