微信小程序錄音與播放


微信小程序錄音與播放

錄音

  • 小程序中提供了兩種錄音的API,舊版已經不在維護,這里我們介紹新版本

wx.getRecorderManager()

  • 獲取全局唯一的錄音管理器,然后錄音都依賴他,而播放錄音則需要內部 audio 上下文 innerAudioContext 對象。

獲取全局唯一的錄音管理器:

const rm = wx.getRecorderManager(); // 創建全局唯一的錄音管理器實例
rm.onError((err) => {
      // 錄音失敗的回調處理
	  console.log(err);
    });
rm.onStop((res) => {
      // 停止錄音之后,把錄取到的音頻放在res.tempFilePath
      this.setData({
        src: res.tempFilePath 
      })
      console.log(res.tempFilePath )
    });

開始錄音:

console.log("錄音開始")
const options = {
	duration: 60000, //錄音的時長,單位 ms
	sampleRate: 44100, //采樣率
	numberOfChannels: 1, //錄音通道數
	encodeBitRate: 192000, //編碼碼率
	format: 'mp3', //音頻格式
	frameSize: 50 //指定幀大小,單位 KB
}
rm.start(options)

結束錄音:

console.log("錄音結束");
rm.stop()

播放音頻:

let myaudio = wx.createInnerAudioContext(); // 創建audio對象
myaudio.onError((res) => {
	// 播放音頻失敗的回調
	console.log(res)
})
myaudio.src = this.data.src;  // 這里可以是錄音的臨時路徑
myaudio.play();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM