Newer
Older
miniprogram / pages / camera / camera.js
bello on 12 May 2020 4 KB camera
const recorderManager = wx.getRecorderManager()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    src: '', //拍照后的缓存路径
    tmpThumbPath: '',
    tmpVideoPath: '',
    tmpRecordPath: '',
    position: 'back',
    mode: 'normal',
    flash: 'auto',
    type: 1, //类型: 1-拍照 2-录像 3-录音
    doing: false, //正在录制
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 加载摄像头
    this.ctx = wx.createCameraContext();

    this.setData({
      type: options.type
    })

  },

  /**
   * 用户不允许使用摄像头时
   */
  error(e) {
    console.log(e.detail)
  },

  /**
   * 拍照
   */
  takePhoto() {
    this.ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
            src: res.tempImagePath
          }),

          this.reback(1)
      }
    })
  },

  /**
   * 开始录像
   */
  takeVideo() {
    this.ctx.startRecord({
      success: () => {
        this.setData({
          doing: true,
          tmpThumbPath: '',
          tmpVideoPath: '',
        })
        console.log('startRecord ' + this.data.doing)
      },
      timeoutCallback: (res) => {
        this.setData({
          doing: false,
          tmpThumbPath: res.tempThumbPath,
          tmpVideoPath: res.tempVideoPath,
        })

        this.reback(2)

        console.log('startRecord ' + this.data.doing)
        console.log('timeout callback ' + res.tempThumbPath)
        console.log('timeout callback ' + res.tempVideoPath)
      }
    })
  },

  /**
   * 结束录像
   */
  stopVideo() {
    this.ctx.stopRecord({
      success: (res) => {
        this.setData({
            doing: false,
            tmpThumbPath: res.tempThumbPath,
            tmpVideoPath: res.tempVideoPath,
          }),

          this.reback(2)

      }
    })
  },

  /**
   * 开始录音
   */
  takeRecord() {

    recorderManager.onStart(() => {
      console.log('recorder start')
    })
    recorderManager.onPause(() => {
      console.log('recorder pause')
    })
    recorderManager.onStop((res) => {
      console.log('recorder stop', res)
      const {
        tempFilePath
      } = res
      this.setData({
        doing: false,
        tmpRecordPath: res.tempFilePath,
      })

      this.reback(3)
      // this.data.doing = false
      // this.data.tmpRecordPath = res.tempFilePath
    })
    recorderManager.onFrameRecorded((res) => {
      const {
        frameBuffer
      } = res
      console.log('frameBuffer.byteLength', frameBuffer.byteLength)
    })

    const options = {
      duration: 10000,
      sampleRate: 44100,
      numberOfChannels: 1,
      encodeBitRate: 192000,
      format: 'aac',
      frameSize: 50
    }

    recorderManager.start(options)

    this.setData({
      doing: true,
    })

  },

  stopRecord() {
    this.setData({
      doing: false,
    })
    recorderManager.stop();

    // this.rdm.onStop((res) => {
    //   console.log('record stop ... ' + res.tempFilePath);
    //   this.doing = false;
    //   this.tmpRecordPath = res.tempFilePath;

    //   console.log("path => " + this.tmpRecordPath)
    //   // this.reback(3);
    // });    
    // this.reback(3)


  },

  /**
   * 拍照成功后返回到上一页
   */
  reback(status) {
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页
    var prevPage = pages[pages.length - 2]; //上一页

    if (status == 1) {
      prevPage.setData({
        tmpPhotoPath: this.data.src,
      })
    } else if (status == 2) {
      prevPage.setData({
        tmpThumbPath: this.data.tmpThumbPath,
        tmpVideoPath: this.data.tmpVideoPath
      })
    } else if (status == 3) {
      console.log('333333');
      prevPage.setData({
        tmpRecordPath: this.data.tmpRecordPath
      })
    }

    // 返回
    wx.navigateBack({
      delta: 1
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})