The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission,iphone手机video标签报错

The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission,在当前上下文中,用户代理或平台不允许该请求,可能是因为用户拒绝了权限。具体原因是iphone手机不允许直接video标签自动播放autoplay。
具体解决方案:
// H5调用电脑摄像头API,成功则返回视频流
      navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.mediaDevices.getUserMedia
      if (!navigator.getUserMedia) {
        console.error('浏览器不支持摄像头!!!!!')
        return
      }
      navigator.mediaDevices
        .getUserMedia({
          video: {
            facingMode: { exact: 'environment' },
            width: 1920,
            height: 1080,
          },
        })
        .then((success) => {
          this.isCamera = false
          // 摄像头开启成功
          this.$refs.video.srcObject = success
          // 播放摄像头画面
          const p = this.$refs.video.play()
          if (p !== undefined) {
            p.then(() => {
              this.$refs.video.pause()
              setTimeout(() => {
                this.$refs.video.play()
              }, 0)
            }).catch((e) => {
              console.log(e, 'undefinedPlay报错')
            })
          }
        })
        .catch((error) => {
          console.error('摄像头开启失败,请检查摄像头是否可用!', error)
        })

 

 
 
posted @ 2023-11-14 11:58  刘星Stars  阅读(1394)  评论(0编辑  收藏  举报