在onShow里面自动调用相机组件的摄像功能
发布于 5 年前 作者 guiying92 6235 次浏览 来自 问答

 onShow: function () {

    console.log(‘showView=’, this.data.showView) 

    var that = this;     

 

    that.ctx.startRecord({

      success: function (res) {

        console.log(‘成功!’)

        console.log(res)

        setTimeout(function () {

          that.ctx.stopRecord({

            success: function (res) {

              console.log(‘tempImageVideoPath=’, res.tempVideoPath)

              console.log(‘tempThumbPath=’, res.tempThumbPath)

              that.setData({

                tempVideoPaths: res.tempVideoPath

                tempFilePaths: res.tempThumbPath

              })

            }

          })

        }, 2000);

 

      }

    })

 

  },

  onLoad: function () {

    this.ctx = wx.createCameraContext();

  }

在页面onshow 每次自动调用相机摄像功能,微信开发工具上预览,ios手机打开相机摄像不起作用,但是在模拟机上或者远程调试上都可以,在安卓手机上可以,是不是ios不支持在onshow里面调用相机摄像功能

1 回复

我在onshow里面使用相机组件的时候,会报错Cannot read property ‘mode’ of undefined.之前是可以的,后来更新过一次版本库就一直报这个错,代码如下:

onShow: function () {
    var that = this;
    that.ctx = wx.createCameraContext()
    that.ctx.startRecord({
      success: (res) => {
        console.log('startRecord')
        setTimeout(function () {
          that.ctx.stopRecord({
            success: (res) => {
              that.setData({
                src: res.tempVideoPath
              })
            }
          })
        }, 5000)
      }
    })
 
  },

经过多次尝试,发现在startRecord上包一层一秒延时,能解决这个问题,代码如下:

onShow: function () {
   var that = this;
   that.ctx = wx.createCameraContext()
   setTimeout(function(){
     that.ctx.startRecord({
       success: (res) => {
         console.log('startRecord')
         setTimeout(function () {
           that.ctx.stopRecord({
             success: (res) => {
               that.setData({
                 src: res.tempVideoPath
               })
             }
           })
         }, 5000)
       }
     })
   },1000)
 },

,以为这样可以解决,但是经过后续各种不同的android机型测试,华为的机型会随机性闪退,做到这,发现真的很蛋疼,没事瞎更新,太坑了 。楼主你的问题你解决了么 。

回到顶部