例如:录音需要添加权限提示
点击录音提示弹框
javascript
<van-popup v-model="isShowAudio" style="border-radius: 10px;">
<div style="background: #fff;padding: 16px 20px;width: 70vw;border-radius: 10px;">
<div style="font-weight: 600;padding-bottom: 10px;">麦克风权限说明</div>
<div style="font-size: 14px;line-height: 20px;">为方便您通过语音描述问题提交随手拍工单,需获取麦克风权限。仅在您明示同意后启用,全程保护您的语音信息安全。
</div>
<div style="text-align: right;font-size: 14px;color: #00adff;padding-top: 16px;">
<span @click="isShowAudioChange" style="display: inline-block;padding-right: 16px;">确定</span>
<span @click="isShowAudio = false">取消</span>
</div>
</div>
</van-popup>
async isShowAudioChange() {
// web-view 通行记录是否已经弹出
webViewJs.postMessage({
data: {
action: 'isShowAudio'
}
});
this.isShowAudio = false
this.$wxApi.initVoice().then(async res => {
if (res === 'sq') {
await this.$wxApi.initVoice2()
} else if (res === 'success') {
this.startRecord()
} else {
}
})
},
async startRecordChange() {
if (isPlusEnv()) {
//是否有授权音频
if (this.$wxApi.r) {
this.startRecord()
} else {
// await this.$wxApi.initVoicePhoto()
// plus.storage.setItem('baseURL', baseURL)
if (+this.isShowAudioStore === 1) {
this.startRecord()
} else {
await this.$wxApi.initVoice2()
this.isShowAudio = true
}
}
return
}
this.startRecord()
},
async startRecord() {
if (this.readonly) return
const _this = this
try {
_this.$wxApi.audioRecord(async function (obj, res) {
// obj 音频的信息, res服务上传接口返回的信息
_this.handleChange({
duration: obj.duration,
accessUrl: res.data.data.accessUrl
})
}).catch(() => {
throw new Error('error')
})
} catch (error) {
}
},
export let r = null
// 授权提示
export function initVoice2() {
r = plus.audio.getRecorder()
r.record({ filename: '_doc/audio/' }, function(p) {}, function(e) {})
// setTimeout(() => {
// r && r.stop()
// }, 30)
}
export function audioRecord(cb) {
if (isPlusEnv()) {
return plusAudioRecord(cb)
} else {
return wxAudioRecord(cb)
}
}
function plusAudioRecord(cb) {
plusStartRecord()
plusStop()
return new Promise((resolve, reject) => {
r.record(
{ filename: '_doc/audio/' },
function (p) {
console.log('录音完成:' + p)
plus.io.resolveLocalFileSystemURL(
p,
function (entry) {
recordUpload(
{
path: entry.name,
duration
},
function (fileInfo, uploadRes) {
cb && cb(fileInfo, uploadRes)
resolve(fileInfo)
},
reject()
)
},
function (e) {
reject('读取录音文件错误:' + e.message)
}
)
},
function (e) {
console.log(e);
if (e.message === 'No Permission') {
Vue.toast('为了帮您录制清晰的语音,需要您在设置中允许 [麦克风] 权限')
stopRecord()
}
reject(e)
}
)
})
}