Skip to content Skip to sidebar Skip to footer

Html5 Video Element Request Stay Pending Forever (on Chrome In Mobile) When Toggle Over Front-rare Camera

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 g

Solution 1:

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

<videoid="player"controlsautoplay></video><buttonid="capture">Capture</button><canvasid="canvas"width=320height=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

Post a Comment for "Html5 Video Element Request Stay Pending Forever (on Chrome In Mobile) When Toggle Over Front-rare Camera"