好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

微信小程序 录像 录视频

项目中,需要客户录制一段视频,上传到服务器,找了很久,终于实现了这个功能。微信小程序有两种方式可以实现录制视频。

1.使用相机的CameraContext.startRecord

2.使用官方API:wx.chooseVideo

方法一

wxml


<view class="video">

  <camera wx:if="{{videoSrc.length === 0}}" device-position="font" flash="off" binderror="error" style="width: {{cameraWidth}}px;height: 442rpx;">

  </camera>

  <video style="width: {{cameraWidth}}px; height: 442rpx;" wx:else src="{{videoSrc}}" controls></video>

</view>

  <view class="btn" id='btn-photo' bindtouchstart="handleTouchStart" bindtouchend="handleTouchEnd" bindlongpress="handleLongPress">按住录制</view>

js部分代码


  onLoad: function (options) {

    //调用设置相机大小的方法

    this.setCameraSize();

    this.ctx = wx.createCameraContext();

  },

  /**

   * 获取系统信息 设置相机的大小适应屏幕

   */

  setCameraSize() {

    //获取设备信息

    const res = wx.getSystemInfoSync();

    //获取屏幕的可使用宽高,设置给相机

    this.setData({

      cameraHeight: res.windowHeight,

      cameraWidth: res.windowWidth

    })

    console.log(res)

  },

  /**

   * 开始录像的方法

   */

  startShootVideo() {

    this.setData({

      videoSrc: ''

    })

    console.log("========= 调用开始录像 ===========")

    let that = this

    this.ctx.startRecord({

      timeoutCallback: () => {

      },

      success: (res) => {

      },

      fail() {

        wx.showToast({

          title: '录像失败',

          icon: 'none',

          duration:4000

        })

        console.log("========= 调用开始录像失败 ===========")

      }

    })

  },

  /**

   * 结束录像

   */

  stopShootVideo() {

    wx.hideLoading();

    // console.log("========= 调用结束录像 ===========")

    this.ctx.stopRecord({

      compressed: true, //压缩视频

      success: (res) => {

        console.log(res)

        this.setData({

          videoSrc: res.tempVideoPath

        })

      },

      fail() {

        wx.showToast({

          title: '录像失败',

          icon: 'none',

          duration:4000

        })

        console.log("========= 调用结束录像失败 ===========")

      }

    })

  },


  //touch start 手指触摸开始

  handleTouchStart: function (e) {

    this.setData({

      startTime: e.timeStamp

    })

  },

  //touch end 手指触摸结束

  handleTouchEnd: function (e) {

    // wx.hideLoading();

    let endTime = e.timeStamp;

    //判断是点击还是长按 点击不做任何事件,长按 触发结束录像

    if (endTime - this.data.startTime > 350) {

      //长按操作 调用结束录像方法

      this.stopShootVideo();

    } else {

      this.setData({

        textFlag: ''

      })

    }

  },

  /**

   * 长按按钮进行录像

   */

  handleLongPress: function (e) {

    // 长按方法触发,调用开始录像方法

    this.startShootVideo();

  },

注:由于官方限制,只能录制30秒。


方法二

在js中


wx.chooseVideo({

  sourceType: ['album','camera'],

  maxDuration: 60,

  camera: 'back',

  success(res) {

    console.log(res.tempFilePath)

  }

})

使用wx.chooseVideo拍摄视频或从手机相册中选视频,拍摄界面不能自定义。

如果有什么不清楚的地方,欢迎私信哦.

具体参数说明请参考官方文档

https://developers.weixin.qq.com/miniprogram/dev/api/media/video/wx.chooseVideo.html(链接地址)


查看更多关于微信小程序 录像 录视频的详细内容...

  阅读:4103次