代码之家  ›  专栏  ›  技术社区  ›  shubham kapoor

当切换到前置摄像头时,HTML5视频元素请求永久挂起(在移动设备的chrome上)

  •  0
  • shubham kapoor  · 技术社区  · 4 年前

    我正在开发一个应用程序,用户可以使用前置/稀有摄像头拍摄照片。它工作得很好,但当切换到摄像头前部/罕见时 var playPromise=视频流。play() 已进入等待状态。有些时候,相机可以正常工作,但有时不能。

    这个问题只出现在chrome浏览器中,而不是mozila和firefox中

           try {
                stopWebCam(); // stop media stream when toggle over camera
                stream = await navigator.mediaDevices.getUserMedia({video: true});
                /* use the stream */
                let videoStream = document.getElementById('captureCandidateId');
                videoStream.srcObject = stream;
    
                    // videoStream.play();
                    var playPromise = videoStream.play();
    
                    if (playPromise !== undefined) {
                      playPromise.then(_ => {
                        // Automatic playback started!
                        // Show playing UI.
                                     
                      })
                      .catch(error => {
                        // Auto-play was prevented
                        // Show paused UI.
                      });
                    }
    
                                    };
            } catch(err) {
                /* handle the error */
                
                console.log(err.name + ": " + err.message);
            }
    
        let stopWebCam = function (pictureType) {
    
        setTimeout(()=>{
            let videoStream = document.getElementById('captureCandidateId');
            const stream = videoStream.srcObject;
            if (stream && stream.getTracks) {
                const tracks = stream.getTracks();
                tracks.forEach(function(track) {
                    track.stop();
                });
            }
            videoStream.srcObject = null;
          }, 0)
        }
    
    0 回复  |  直到 4 年前
        1
  •  0
  •   Tejas Gupta    4 年前

    在这里,我为您起草了一段代码,这是一种比您正在尝试的更简单、更小的方法。我只是从视频元素中提取流并将其绘制到画布上。右键单击可下载图像。 注意:示例在StackOverflow中不起作用

    <video id="player" controls autoplay></video>
    <button id="capture">Capture</button>
    <canvas id="canvas" width=320 height=240></canvas>
    <script>
      const player = document.getElementById('player');
      const canvas = document.getElementById('canvas');
      const context = canvas.getContext('2d');
      const captureButton = document.getElementById('capture');
    
      const constraints = {
        video: true,
      };
    
      captureButton.addEventListener('click', () => {
        // Draw the video frame to the canvas.
        context.drawImage(player, 0, 0, canvas.width, canvas.height);
      });
    
      // Attach the video stream to the video element and autoplay.
      navigator.mediaDevices.getUserMedia(constraints)
        .then((stream) => {
          player.srcObject = stream;
        });
    </script>

    如果需要,还可以根据需要进行一些编辑,例如:

    1. 选择要使用的相机
    2. 隐藏视频流
    3. 添加一种更简单的方法将照片下载到设备上
    4. 如果你有一张照片,你也可以添加一个功能,直接将照片上传到服务器上