navigator.mediaDevices.enumerateDevices() not disp

2019-07-13 11:26发布

问题:

I am working on media control functionality. I am displaying device name to select from a dropdown and it's working fine on chrome but on firefox it will not fetching label or device name.

回答1:

navigator.mediaDevices.enumerateDevices() will return an empty label attribute value in the media device info if the respective permissions are not granted.

To make it work, I placed this function after all of the media permissions have been granted so it returns a label attribute value as well.



回答2:

navigator.mediaDevices.enumerateDevices() returns a promise that's fulfilled with an array of MediaDeviceInfo instances.

It worked for me in Firefox 56.0 (64-bit).

You can do something like this:

navigator.mediaDevices.enumerateDevices()
.then((data) => {
  console.log('data', data);
})
.catch((err) => {
  console.log('error getting MediaDeviceInfo list', err);
});

where data is the array that contains the list of all MediaDeviceInfo instances.

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