需求
app获取录音权限权限, 实现录音并且播放功能
实现
一. 使用permission获取录音权限
原博 : https://www.wanjunshijie.com/note/uniapp/3203.html
1.1 manifest.json 配置权限 android.permission.RECORD_AUDIO
1.2 permision使用和下载 ( 自行百度搜索即可 )
1.3 获取录音权限( 若未打开, 会自动弹窗 )
import permision from "@/utils/permission.js"open() {let env = uni.getSystemInfoSync().platformif (env === 'android') {permision.requestAndroidPermission('android.permission.RECORD_AUDIO').then((e) => {if (e === -1) {uni.showToast({title: '您已经永久拒绝录音权限,请在应用设置中手动打开',icon: 'none',})} else if (e === 0) {uni.showToast({title: '您拒绝了录音授权',icon: 'none',})} else if (e === 1) {this.show = true} else {uni.showToast({title: '授权返回值错误',icon: 'none',})}}).catch((err) => {uni.showToast({title: '拉起录音授权失败',icon: 'none',})})} else if (env === 'ios') {if (permision.judgeIosPermission("record"))this.show = trueelseuni.showToast({title: '您拒绝了录音授权,请在应用设置中手动打开',icon: 'none',})}},
二. 开始 / 停止 / 播放 录音
文档 : https://uniapp.dcloud.net.cn/api/media/record-manager.html#getrecordermanager
<view><button @click="startRecord">开始录音</button><button @click="endRecord">停止录音</button><button @click="playVoice">播放录音</button></view>
const recorderManager = uni.getRecorderManager();const innerAudioContext = uni.createInnerAudioContext();innerAudioContext.autoplay = true;startRecord() {console.log('开始录音');recorderManager.start();},endRecord() {console.log('录音结束');recorderManager.stop()recorderManager.onStop((res) => {console.log(res, '开始录音的回调')this.voicePath = res.tempFilePath;})},playVoice() {console.log('播放录音');if (this.voicePath) {innerAudioContext.src = this.voicePath;innerAudioContext.play();}},