MediaDevices.enumerateDevices()

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

The MediaDevices.enumerateDevices() method collects information about the media input and output devices available on the system.

Syntax

navigator.mediaDevices.enumerateDevices()
.then(function(MediaDeviceInfo) { ... })

Returns

Returns a Promise that will be fulfilled with an array of MediaDeviceInfo instances, which contain information on the available media input and output devices, if enumeration is successful.

Example

Here's an example of using 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);
});

This might produce:

videoinput: id = csO9c0YpAf274OuCPUA53CNE0YHlIr2yXCi+SqfBZZ8=
audioinput: id = RKxXByjnabbADGQNNZqLVLdmXlS0YkETYCIbg+XxnvM=
audioinput: id = r2/xw1xUPIyZunfV1lGrKOma5wTOvCkWfZ368XCndm0=

or if one or more MediaStreams are active or persistent permissions are granted:

videoinput: FaceTime HD Camera (Built-in) id=csO9c0YpAf274OuCPUA53CNE0YHlIr2yXCi+SqfBZZ8=
audioinput: default (Built-in Microphone) id=RKxXByjnabbADGQNNZqLVLdmXlS0YkETYCIbg+XxnvM=
audioinput: Built-in Microphone id=r2/xw1xUPIyZunfV1lGrKOma5wTOvCkWfZ368XCndm0=

Permissions

To use enumerateDevices() in an installable app (for example, a Firefox OS app), you need to specify one or both of the following fields inside your manifest file:

"permissions": {
  "audio-capture": {
    "description": "Required to capture audio using getUserMedia()"
  },
  "video-capture": {
    "description": "Required to capture video using getUserMedia()"
  }
}

See permission: audio-capture and permission: video-capture for more information.

Specifications

Specification Status Comment
Media Capture and Streams
The definition of 'mediaDevices.enumerateDevices' in that specification.
Editor's Draft Initial definition.

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 45.0 [1] (Yes) 39 No support No support No support
Feature Android Android Webview Edge Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support No support No support (Yes) 39 39 No support No support No support No support

[1] Behind a flag.

Chrome and Opera compatibility

  • This interface is available in Chrome and Opera through the adapter.js polyfill.

See also

Document Tags and Contributors

 Contributors to this page: chrisdavidmills, Dabr, libbymc, teoli, myakura, jpmedley, jwhitlock, Jib, julienw
 Last updated by: chrisdavidmills,