HTML5 video element request stay pending forever (on chrome in mobile) when toggle over front-rare camera

482 views Asked by At

I'm developing an app where users can capture photo using a front/rare camera. it working perfectly but when toggle over camera front/rare var playPromise = videoStream.play() is gone in pending state. some times promise get resolve, the camera is working sometimes not.

this issue occurs only in chrome browser not in mozila and 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)
    }
2

There are 2 answers

1
Tejas Gupta On

Here, I drafted a piece of code for you, this is much more simple and smaller approach than what you are trying to do. I am just taking the stream from the video element and drawing it to canvas. Image can be downloaded by right clicking. NOTE: Example does not work in 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>

If you want, you can also make some edits according to your needs, like:

  1. Choose which camera to use
  2. Hide the video stream
  3. Add a easier method to download the photo on your device
  4. You can also add a functionality to upload the photo straight to the server if you have one
1
Yotako On

check if the video resolution is exceed 4k(3840x2160). if true, try downscale.