-1

There's a web page for mobile and from js I can get access to the camera. One case I need to support is when there's no camera. So last week for such case I was getting exception sth like 'Could not start a video source' and added error handler. Today an upload dialog box is opened Now I'm testing it on pc on chrome but soon want to test it on android as well

  • How to catch edge cases like no camera/no permission/other kind of errors so it'll be working on ios/android devices and desktop
  • I don't want any upload dialog box to be displayed. Is there any solution?
ra74
  • 129
  • 1
  • 10

1 Answers1

2

You can check if any video devices type available or not using enumerateDevices() function available in navigator.

const cameraAvailable = (devices) => {
    const videoDevice = devices.filter((d) => d.kind === "videoinput");

    if (videoDevice.length > 0) {
      console.log("camera available");
      return true;
    } else {
      console.log("No camera available");
      return false;
    }
  };
  const getMediaDevices = async () => {
    try {
      const devices = await navigator.mediaDevices.enumerateDevices();
      if(cameraAvailable(devices)){
        // handle stuff with camera
      }
    } catch (error) {
      console.log(error);
    }
  };
Harsh Mangalam
  • 1,116
  • 1
  • 10
  • 19
  • ok, thanks that's working. Just wondering why the behaviour has changed. Was it due to weekend windows and chrome update or so – ra74 Feb 14 '22 at 11:22