Enumerate mediaDevices empty javascript

478 Views Asked by At

I want to select which camera to use. I ve found that exists: enumerateDevices()

https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices

if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
  console.log("enumerateDevices() not supported.");
  return;
}

// List cameras and microphones.

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

I have tried but it doesnt return any camera device.

1

There are 1 best solutions below

0
On

I tested your code, it works fine. it returns the mediaDevices list to me.

audioinput: id = default
audioinput: id = communications
audioinput: id = 59392f81738870b09dec51e4375320b0a0ae2f7c44a2683272491e708c3c5cf4
videoinput: id = ae367efe808e935504983bda9831593a5324b3d5e9b79f8801eb9d9d55406531
 audiooutput: id = default
 audiooutput: id = communications
 audiooutput: id = 65235ec7e9c60747b6c24f3de8305f2ceead65432841a7706d57dae87412e1b

I just have an error that appears in your "if" : "Illegal return statement". You have to remove the return.

if (!navigator.mediaDevices ||!navigator.mediaDevices.enumerateDevices) {
  console.log ("enumerateDevices () not supported.");
}