need
The app obtains the recording permission permission, realizes the recording and playback function
accomplish
1. Use permission to obtain recording permission
Original blog: https://www.wanjunshijie.com/note/uniapp/3203.html
1.1 Android: manifest.json configuration permission android.permission.RECORD_AUDIO
**ios : manifest.json module configuration Record **
1.2 Permision use and download (you can search on Baidu yourself)
1.3 Obtain recording permission
import permision from "@/utils/permission.js"
open() {
let env = uni.getSystemInfoSync().platform
if (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 = true
else
uni.showToast({
title: '您拒绝了录音授权,请在应用设置中手动打开',
icon: 'none',
})
}
},
iOS opens the microphone permission and also needs to apply for the microphone permission requestRecordPermission
// 当麦克风权限未开启时, 申请麦克风权限
var avaudiosession = plus.ios.import("AVAudioSession");
var avaudio = avaudiosession.sharedInstance();
avaudio.requestRecordPermission(()=>{
console.log('申请麦克风权限');
});
The screenshot below is from: https://ask.dcloud.net.cn/question/142395
Simple steps to understand microphone permission application and acquisition:
- Configure the menifest.json module and permissions (need to repackage after configuration)
- Android
calls plus.android.requestPermissions([permissionID]) to query permissions. If it is not opened, a pop-up window will ask
ios
to call recordPermission() to query permissions. If no selection is made, request permission requestRecordPermission(). If permission has been applied, but If the user refuses, jump to "Settings"
2. Start/stop/play recording
Documentation: 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();
}
},