1.可以使用:plus.navigator.checkPermission
检查运行环境的权限
2.如果是"undetermined"表示程序未确定是否可使用此权限,此时调用对应的API时系统会弹出提示框让用户确认:plus.audio.getRecorder()
<template><view class="index"><view class="title">虚拟人类智能AI已唤醒,可以随时开始聊天啦</view><view class="lists"><view class="item" v-for="(item,index) in 13" :key="index"><image src="../../static/img/4.png" mode=""></image><view class="right"><view class="name">智能AI</view><view class="time">11-20 14:18:27</view><view class="text">内容内容长度内长度长度长度长度</view></view></view></view><view class="footers"><view class="submit" @longpress="star" @touchend="stop"><image src="../../static/img/5.png" mode=""></image><view class="text1">按住说话</view><view class="text2">Copyright @ 2023 相关内容由人工智能技术生成</view></view></view></view>
</template><script>const recorderManager = uni.getRecorderManager();const innerAudioContext = uni.createInnerAudioContext();innerAudioContext.autoplay = true;export default {data() {return {audioSrc:'',status:0,// 1录制中,0已结束}},onLoad() {var quanxianData = plus.navigator.checkPermission('RECORD');if(quanxianData !== 'authorized'){// 先调用一次,提示开启权限recorderManager.start({format:'PCM'});setTimeout(()=>{uni.hideLoading();this.status = 0;recorderManager.stop();},500)}// 录音停止事件recorderManager.onStop((res)=> {if(this.status == 1){console.log(res);this.audioSrc = res.tempFilePath;this.status = 0;}});},methods: {star() {// 长按录制前 再次判断当前权限状态。var quanxianData = plus.navigator.checkPermission('RECORD');if(quanxianData !== 'authorized'){uni.showToast({title: '请前往设置,开启录音权限',icon: 'none'});}else{if(this.status == 1) returnuni.showLoading({title: '请说出您的问题',mask:true});this.status = 1;recorderManager.start({format:'PCM'});}},stop() {uni.hideLoading();recorderManager.stop();},}}
</script>