我写在 onLoad中调用, 页面渲染后,直接提示麦克风授权,当全部点取消时,要使用可以直接调用impower()
onLoad: function (options){
this.impower()
},
methods: {
//授权
impower(){
var t = this;
wx.authorize({
scope: 'scope.record',
success (res){
console.log("录音授权成功",res);
//app.globalData.audioplay = true
// 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问
// wx.startRecord();
t.recorderManager = uni.getRecorderManager();
t.innerAudioContext = uni.createInnerAudioContext();
// 为了防止苹果手机静音无法播放
uni.setInnerAudioOption({
obeyMuteSwitch: false
})
t.innerAudioContext.autoplay = true;
console.log("uni.getRecorderManager()",uni.getRecorderManager())
t.recorderManager.onStop(function (res) {
console.log('recorder stop' + JSON.stringify(res));
t.voicePath = res.tempFilePath;
});
},
fail(){
console.log("第一次录音授权失败");
wx.showModal({
title: '提示',
content: '您未授权录音,功能将无法使用',
showCancel: true,
confirmText: "授权",
confirmColor: "#AF1F25",
success(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) {
console.log("不知道打印的啥?")
},
})
} else {
//第二次才成功授权
console.log("设置录音授权成功");
//app.globalData.audioplay = true;
t.recorderManager = uni.getRecorderManager();
t.innerAudioContext = uni.createInnerAudioContext();
// 为了防止苹果手机静音无法播放
uni.setInnerAudioOption({
obeyMuteSwitch: false
})
t.innerAudioContext.autoplay = true;
console.log("uni.getRecorderManager()",uni.getRecorderManager())
t.recorderManager.onStop(function (res) {
console.log('recorder stop' + JSON.stringify(res));
t.voicePath = res.tempFilePath;
});
}
},
fail: function () {
console.log("授权设置录音失败");
}
})
} else if (res.cancel){
console.log("cancel");
}
} ,
fail (){
console.log("openfail");
}
})
}
})
},
}