How can we access the list of cameras in GetUserMedia?

To access a list of cameras, you can use the navigator.mediaDevices.enumerateDevices() method. This method will return a Promise object, which can be used to obtain the device list with the .then() method. Within the list, you can determine if the device is a camera or microphone based on the kind property.

Here is a simple example code:

navigator.mediaDevices.enumerateDevices()
  .then(function(devices) {
    devices.forEach(function(device) {
      if (device.kind === 'videoinput') {
        console.log(device.label + " - " + device.deviceId);
      }
    });
  })
  .catch(function(err) {
    console.log(err.name + ": " + err.message);
  });

This code will list all labels and device IDs for camera devices.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds