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

JavaScript
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().

JavaScript
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:

JavaScript
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:

JavaScript
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:

JavaScript
"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 Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 45.0 [1] 39 No support No support No support
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support No support No support 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

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/mediadevices/enumeratedevices

API Experimental MediaDevices Method Reference WebRTC