小程序中提供了兩種錄音的API
- 舊版錄音功能
首先啟動(dòng)錄音,然后停止錄音即可拉到音頻的臨時(shí)地址
啟動(dòng)錄音:
var that = this;
wx.startRecord({
success: function (res) {
// 調(diào)用了停止錄音接口就會(huì)觸發(fā)這個(gè)函數(shù)蜀漆,res.tempFilePath為錄音文件臨時(shí)路徑
var tempFilePath = res.tempFilePath
that.setData({
src: tempFilePath
})
},
fail: function (res) {
//錄音失敗的處理函數(shù)
}
})
停止錄音:
wx.stopRecord()
播放錄音:
wx.playVoice({
filePath: src // src可以是錄音文件臨時(shí)路徑
})
- 新版錄音
獲取全局唯一的錄音管理器,然后錄音都依賴他扭吁,而播放錄音則需要內(nèi)部 audio 上下文 innerAudioContext 對(duì)象让腹。
獲取全局唯一的錄音管理器:
var that = this;
this.recorderManager = wx.getRecorderManager();
this.recorderManager.onError(function(){
// 錄音失敗的回調(diào)處理
});
this.recorderManager.onStop(function(res){
// 停止錄音之后赡若,把錄取到的音頻放在res.tempFilePath
that.setData({
src: res.tempFilePath
})
console.log(res.tempFilePath )
});
開(kāi)始錄音:
this.recorderManager.start({
format: 'mp3' // 如果錄制acc類型音頻則改成aac
});
結(jié)束錄音:
this.recorderManager.stop()
播放音頻:
this.innerAudioContext = wx.createInnerAudioContext();
this.innerAudioContext.onError((res) => {
// 播放音頻失敗的回調(diào)
})
this.innerAudioContext.src = this.data.src; // 這里可以是錄音的臨時(shí)路徑
this.innerAudioContext.play()
- DEMO地址