微信小程序把玩(三十三)Record API

這裏寫圖片描述

其實這個API也挺奇葩的,錄音結束後success不走,complete不走,fail也不走, 不知道是不是因爲電腦測試的原因,只能等公測或者等他們完善。以後再測和補充吧!!!!

主要屬性:

wx.startRecord(object)

這裏寫圖片描述

手動調用wx.stopRecord()停止錄音

wxml

<!--用於記錄時間-->
<text>{{formatRecordTime}}</text>
<button type="primary" bindtap="listenerButtonStartRecord">開始錄音</button>
<button type="primary" bindtap="listenerButtonStopRecord">結束錄音</button>

js

var util = require('../../../utils/util.js')
var interval
Page({
  data:{
      //錄音顯示類型
    formatRecordTime: '00:00:00',
    //計數
    recordTime: 0,
  },

  onLoad:function(options){
    // 頁面初始化 options爲頁面跳轉所帶來的參數
  },
  /**
   * 監聽按鈕點擊開始錄音
   */
  listenerButtonStartRecord: function() {
      that = this;
      interval = setInterval(function() {
      that.data.recordTime += 1     
      that.setData({
          //格式化時間顯示
          formatRecordTime: util.formatTime(that.data.recordTime)
      })    
      }, 1000)
      wx.startRecord({
          success: function(res) {
              console.log(res)
              that.setData({
                  //完成之後重新繪製
                  formatRecordTime: util.formatTime(that.data.recordTime)
              })
          },
          /**
           * 完成清除定時器
           */
          complete: function() {
              clearInterval(interval)
          }
      })
  },
  /**
   * 監聽手動結束錄音
   */
  listenerButtonStopRecord: function() {
    wx.stopRecord();
    clearInterval(interval);
    this.setData({
        formatRecordTime: '00:00:00',
        recordTime: 0
    })
  },
  onReady:function(){
    // 頁面渲染完成
  },
  onShow:function(){
    // 頁面顯示
  },
  onHide:function(){
    // 頁面隱藏
  },
  /**
   * 當界面關閉時停止定時器關閉錄音
   */
  onUnload:function(){
    // 頁面關閉
    wx.stopRecord()
    clearInterval(interval)
  }
})
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章