微信小程序錄音開發

第一步,

<view class='container'>
  <button bindtap='startRecording'>開始錄音</button>
  <button bindtap='pauseRecording'>暫停錄音</button>
  <button bindtap='resumeRecording'>繼續錄音</button>
  <button bindtap='stopRecording'>結束錄音</button>
  <view>
    <view>時長:{{audioMsg.duration}}</view>
    <view>文件大小:{{audioMsg.fileSize}}</view>
    <view>臨時路徑:{{audioMsg.tempFilePath}}</view>
  </view>
</view>

第二步

var app = getApp();
var recorderManager = wx.getRecorderManager(); //錄音管理

var options = {
  duration: 300000,
  sampleRate: 16000,
  numberOfChannels: 1,
  encodeBitRate: 48000,
  format: 'mp3',//這裏是生成的音頻文件格式
  frameSize: 50,
};
Page({

  data: {
    audioMsg:{}
  },
  onLoad(){
    var that = this
    //監聽錄音結束 並返回本地MP3文件
    recorderManager.onStop((res) => {
      that.setData({
        audioMsg:res
      })
      console.log(res)
    })
  },
  //開始錄音
  startRecording(){
    recorderManager.start(options)
    console.log('開始')
  },
  //暫停錄音
  pauseRecording() {
    recorderManager.pause();
    console.log('暫停')
  }, 
  //繼續錄音
  resumeRecording(){
    recorderManager.resume();
  },
  //結束錄音
  stopRecording() {
    recorderManager.stop();
  }
})

其實很簡單,並沒有那麼複雜,拿到臨時地址,上傳服務器就可以了,

如果需要延伸的小夥伴可以私聊我,

有幫助到大家的記得關注噢~

 

喜歡上方小程序,需要源碼的,私信小編留下郵箱。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章