商城系統(tǒng) 注冊(cè)

微信小程序錄音和播放錄音的開發(fā)

2020-09-27|HiShop
導(dǎo)讀:關(guān)于微信小程序錄音與播放錄音的開發(fā),小程序中提供了兩種錄音的API。...

  關(guān)于微信小程序錄音與播放錄音的開發(fā),小程序中提供了兩種錄音的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

微信小程序錄音和播放錄音的開發(fā)

  that.setData({

  src: res.tempFilePath

  })

  console.log(res.tempFilePath )

  });

  開始錄音:

  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地址

  github:https://github.com/yubang/appletRecordDemo
 

  HiShop小程序工具提供多類型商城/門店小程序制作,可視化編輯 1秒生成5步上線。通過(guò)拖拽、拼接模塊布局小

  程序商城頁(yè)面,所看即所得,只需要美工就能做出精美商城。

電話咨詢 預(yù)約演示 0元開店