美文网首页
uniapp调用createCameraContext报错

uniapp调用createCameraContext报错

作者: 清风昙 | 来源:发表于2022-03-18 16:09 被阅读0次

使用uniapp框架开发项目,调用createCameraContext实现录像功能,发现手机端偶尔会出现录像失败,调试查看发现录像失败时会报错operateCamera:fail operate fail。
出现上述报错是createCameraContext的潜在bug,官方也没给出更好的解决方式,目前更好的解决这问题,就是当报错出现时重新启动下startRecord(),基本能处理报错operateCamera:fail operate fail。代码如下:

onLoad() {
  this.ctx = uni.createCameraContext();
}
methods: {
   startRecord() {
      let ctx = this.ctx;
      setTimeout(() => {
    ctx.startRecord({
      timeoutCallback : (res) => { // 录像超30S或onHide时结束录像
         that.videoSrc = res.tempVideoPath
         console.log(`startRecord timeoutCallback 30s videoInfo:${JSON.stringify(res)}`)
       },
       success: (res) => {
        console.log('startRecord success')
        },
        fail: (err) => {
        console.log(`startRecord fail:${JSON.stringify(err)}`)
            that.startRecord() // 启动失败,重新
         }
      })
      }, 100)
   },
   stopRecord () {
    var that = this
    let ctx = this.ctx
    setTimeout(() => {
        ctx.stopRecord({
        compressed: true, // 是否启动视频压缩
        success: (res) => {
            that.videoSrc = res.tempVideoPath
            console.log(`stopRecord1 success videoInfo:${JSON.stringify(res)}`)
        },
        fail: (err) => {
            console.log(`stopRecord fail videoInfo:${JSON.stringify(err)}`)
        }
        })
        }, 200)
    },
}

相关文章

网友评论

      本文标题:uniapp调用createCameraContext报错

      本文链接:https://www.haomeiwen.com/subject/cwzgdrtx.html