小程式實現錄音功能
阿新 • • 發佈:2020-09-23
本文例項為大家分享了小程式實現錄音功能的具體程式碼,供大家參考,具體內容如下
首先判斷許可權
getPermission: function() { var that = this; wx.getSetting({ success(res) { console.log(res.authSetting) if (res.authSetting["scope.record"] === false) { wx.showModal({ title: '是否錄音',content: '是否錄音',success: function (tip) { if (tip.confirm) { wx.openSetting({ success: function (data) { if (data.authSetting["scope.record"] === true) { wx.showToast({ title: '授權成功',icon: 'success',duration: 1000 }) that.startLuYin() //授權成功之後,再呼叫chooseLocation選擇地方 } else { wx.showToast({ title: '授權失敗',duration: 1000 }) } } }) } } }) }else{ that.startLuYin() } } }) },
授權成功後開始錄音
startLuYin(){ const options = { duration: 10000 * 6 * 10,//指定錄音的時長,單位 ms sampleRate: 16000,//取樣率 numberOfChannels: 1,//錄音通道數 encodeBitRate: 96000,//編碼位元速率 format: 'mp3',//音訊格式,有效值 aac/mp3 frameSize: 50,//指定幀大小,單位 KB } //開始錄音 recorderManager.start(options); recorderManager.onStart(() => { console.log('recorder start'); Countdown(this); //開始計時 }); //錯誤回撥 recorderManager.onError((res) => { console.log('recorder出錯:' + res); console.log(res); clearTimeout(timer); //出錯時停止計時 }) },
暫停錄音
// 暫停錄音 pause: function() { var that = this; recorderManager.pause() recorderManager.onPause((res) => { console.log(res) console.log('暫停錄音') clearTimeout(timer); }) },
繼續錄音
//繼續錄音 jixu: function() { var that = this; recorderManager.resume() Countdown(that); //開始計時 recorderManager.onResume((res) => { }) },
停止錄音
//停止錄音 stop: function() { recorderManager.stop(); recorderManager.onStop((res) => { this.tempFilePath = res.tempFilePath; console.log('停止錄音',res.tempFilePath) clearTimeout(timer); }) },
播放聲音
//播放聲音 play: function() { innerAudioContext.autoplay = true innerAudioContext.src = this.tempFilePath,innerAudioContext.onPlay(() => { console.log('開始播放') }) innerAudioContext.onError((res) => { console.log(res.errMsg) console.log(res.errCode) }) },
// 倒計時 function Countdown(that) { timer = setTimeout(function() { console.log("----secondes----" + formatSeconds(secondes)); secondes++; if (secondes >= 600) { recorderManager.stop(); clearTimeout(timer); } that.setData({ times: formatSeconds(secondes) }); Countdown(that); },1000); };
// 時間展示 function formatSeconds(value) { var secondTime = parseInt(value); // 秒 var minuteTime = 0; // 分 var hourTime = 0; // 小時 if (secondTime > 60) { //如果秒數大於60,將秒數轉換成整數 //獲取分鐘,除以60取整數,得到整數分鐘 minuteTime = parseInt(secondTime / 60); //獲取秒數,秒數取佘,得到整數秒數 secondTime = parseInt(secondTime % 60); //如果分鐘大於60,將分鐘轉換成小時 if (minuteTime > 60) { //獲取小時,獲取分鐘除以60,得到整數小時 hourTime = parseInt(minuteTime / 60); //獲取小時後取佘的分,獲取分鐘除以60取佘的分 minuteTime = parseInt(minuteTime % 60); } } var result; //時間的展示方式為00:00 if (secondTime < 10) { result = "0" + parseInt(secondTime); } else { result = "" + parseInt(secondTime); } if (minuteTime > 0) { if (minuteTime < 10) { result = "0" + parseInt(minuteTime) + ":" + result; } else { result = "" + parseInt(minuteTime) + ":" + result; } } else { result = "00:" + result; } //由於限制時長最多為三分鐘,小時用不到 if (hourTime > 0) { result = "" + parseInt(hourTime) + ":" + result; } return result; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。