錄音
onLoad(options) { let that=this this.recorderManager = uni.getRecorderManager(); //創建錄音管理 }, methods:{ //開始錄音 startRecord(index){ let that=this uni.authorize({ scope:"scope.record", success(){ uni.showToast({ icon:"none", title:"開始錄音" }) that.recorderManager.start(); that.recorderManager.onStart() console.log("錄音執行") }, fail() { uni.showModal({ title: '提示', content: '您未授權錄音,功能將無法使用', showCancel: true, confirmText: "授權", confirmColor: "#52a2d8", success(res) { if(res.confirm){ uni.openSetting({ }) }else{ } } }) } }) }, //結束錄音 endRecord(index){ let that=this uni.stopRecord({ success:function(e){ that.recorderManager.stop(); that.recorderManager.onStop((res=>{ that.audioTempFile=res.tempFilePath that.uploadAudio(index) })) } }) } }
播放音頻文件
onLoad(options) { let innerAudioContext = uni.createInnerAudioContext(); //創建音頻文件 innerAudioContext.autoplay = false; //不自動播放 innerAudioContext.src = this.audioSrc; //音頻文件鏈接 this.innerAudioContext=innerAudioContext }, methods:{ //開始音頻文件 startAudio(){ this.innerAudioContext.play(() => { console.log('開始播放'); }); this.innerAudioContext.onError((res) => { console.log(res.errMsg); console.log(res.errCode); }); }, //結束音頻 endAudio(){ let that=this uni.showToast({ icon:"none", title:"已結束" }) this.innerAudioContext.stop(() => { console.log('已暫停'); }); this.innerAudioContext.onError((res) => { console.log(res.errMsg); console.log(res.errCode); }); }, //暫停音頻文件 pauseAudio(desc){ let that=this uni.showToast({ icon:"none", title:desc }) this.innerAudioContext.pause(() => { console.log('已暫停'); }); this.innerAudioContext.onError((res) => { console.log(res.errMsg); console.log(res.errCode); }); } }