<view open-type="openSetting" bindtap="getRecord" >
//录音计时器
recordingTimer: function() {
var that = this;
//将计时器赋值给setInter
that.data.setInter = setInterval(
function() {
var time = that.data.recordingTimeqwe + 1;
that.setData({
recordingTimeqwe: time
})
}, 1000);
},
// 录音开始
recordStart: function() {
let that = this;
const options = {
duration: 60000, //指定录音的时长,单位 ms,最大为10分钟(600000),默认为1分钟(60000)
format: 'mp3', //音频格式,有效值 aac/mp3
}
//开始录音计时
that.recordingTimer();
//开始录音
recorderManager.start(options);
//屏幕常亮
wx.setKeepScreenOn({
keepScreenOn: true
})
recorderManager.onStart(() => {
console.log('。。。开始录音。。。')
});
//错误回调
recorderManager.onError((res) => {
console.log("录音错误", res);
})
},
//录音结束
recordEnd: function() {
let that = this;
console.log("结束录音")
recorderManager.stop();
//取消屏幕常亮
wx.setKeepScreenOn({
keepScreenOn: false
})
recorderManager.onStop((res) => {
const {duration} = res; //播放时长
const {fileSize} = res; //文件大小
console.log("大小", fileSize)
const {tempFilePath } = res; // 文件临时路径
//结束录音计时
clearInterval(that.data.setInter);
//上传片段录音
wx.uploadFile({
url: '', //要上传的url
filePath: tempFilePath,
name: "file", //后台要绑定的名称
header: {
"Content-Type": "multipart/form-data"
},
//参数绑定
formData: {
userId: wx.getStorageSync('userId'),
duration: duration,
size: fileSize
},
success: function(res) {
console.log("录音保存成功");
},
fail: function(res) {
console.log("录音保存失败");
}
})
})
},
//录音取消操作
cancelRecord: function() {
let that = this;
console.log("结束录音")
recorderManager.stop();
//取消屏幕常亮
wx.setKeepScreenOn({
keepScreenOn: false
})
recorderManager.onStop((res) => {
const {duration } = res; //播放时长
const {fileSize} = res; //文件大小
const {tempFilePath } = res;
//结束录音计时
clearInterval(that.data.setInter);
wx.showToast({
title: '录音已取消',
duration: 800,
icon: 'none'
})
})
},
//录音授权
getRecord: function() {
var that = this;
wx.getSetting({
success(res) {
console.log("录音授权", res);
if (!res.authSetting['scope.record']) {
//调取小程序新版授权页面
wx.authorize({
scope: 'scope.record',
success() {
console.log("录音授权成功");
that.setData({
status: 2
})
that.recordStart()
},
fail() {
wx.showModal({
title: '提示',
content: '您未授权录音,功能将无法使用',
showCancel: true,
confirmText: "授权",
confirmColor: "#52a2d8",
success: function(res) {
if (res.confirm) {
//确认则打开设置页面(重点)
wx.openSetting({
success: (res) => {
console.log("重新授权", res.authSetting);
if (!res.authSetting['scope.record']) {
//未设置录音授权
console.log("未设置录音授权");
wx.showModal({
title: '提示',
content: '您未授权录音,功能将无法使用',
showCancel: false,
success: function(res) {
},
})
} else {
//第二次才成功授权
console.log("第二次设置录音授权成功");
that.recordStart()
}
},
fail: function() {
console.log("授权设置录音失败");
}
})
} else if (res.cancel) {
console.log("cancel");
}
},
fail: function() {
console.log("openfail");
}
})
}
})
} else {
that.recordStart();
}
}
})
},