小程序中开启录音功能(使用wepy)
作者:互联网
1.检查是否获取到录音权限---获取录音权限
onLoad(options, data) { this.judgeRecord().then(res=>{ if(res==true) { this.recordInit = true; this.$apply(); } }) this.$apply(); }
1 judgeRecord() { 2 return new Promise(function (resolve, reject) { 3 wx.getSetting({ 4 success(res) { 5 if (!res.authSetting['scope.record']) { 6 if (res.authSetting['scope.record'] == undefined) { 7 wx.authorize({ 8 scope: 'scope.record', 9 success() { 10 // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问 11 // wx.startRecord() 12 resolve(true) 13 }, 14 fail() { 15 resolve(false) 16 } 17 }) 18 }else{ 19 wx.showModal({ 20 title: '请求授权录音权限', 21 content: '需要获取您的录音权限,请确认授权', 22 success: function (res) { 23 if (res.cancel) { 24 wx.showToast({ 25 title: '拒绝授权', 26 icon: 'none', 27 duration: 1000 28 }) 29 resolve(false) 30 } else if (res.confirm) { 31 wx.openSetting({ 32 success: function (dataAu) { 33 if (dataAu.authSetting["scope.record"] == true) { 34 wx.showToast({ 35 title: '授权成功', 36 icon: 'success', 37 duration: 1000 38 }) 39 resolve(true) 40 //再次授权,调用wx.getLocation的API 41 } else { 42 wx.showToast({ 43 title: '授权失败', 44 icon: 'none', 45 duration: 1000 46 }) 47 resolve(false) 48 } 49 } 50 }) 51 } 52 } 53 }) 54 } 55 56 }else{ 57 resolve(true) 58 } 59 } 60 }); 61 }) 62 63 }
2.初始加载如果用户拒绝授权,则在需要操作录音的位置继续调用授权
onTouchStart(e) { this.$apply(); if (!this.recordInit) { this.judgeRecord(); return false; } },
标签:resolve,success,res,wepy,录音,开启,true,wx 来源: https://www.cnblogs.com/fairy62/p/12612064.html