MediaDevices.enumerateDevices()

The MediaDevices method enumerateDevices() requests a list of the available media input and output devices, such as microphones, cameras, headsets, and so forth. The returned Promise is resolved with a MediaDeviceInfo array describing the devices.

Syntax

var enumeratorPromise = navigator.mediaDevices.enumerateDevices();

Return value

A Promise that receives an array of MediaDeviceInfo objects when the promise is fulfilled. Each object in the array describes one of the available media input and output devices.

If enumeration fails, the promise is rejected.

Example

Here's an example of using enumerateDevices(). It simply outputs a list of the device IDs, with their labels if available.

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=

Specifications

Specification Status Comment
Media Capture and Streams
The definition of 'mediaDevices: enumerateDevices' in that specification.
Candidate Recommendation Initial definition.

Browser compatibility

DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
enumerateDevicesChrome Full support 47Edge Full support 12Firefox Full support 63
Notes Disabled
Full support 63
Notes Disabled
Notes Prior to Firefox 63, enumerateDevices() only returned input devices. Starting with Firefox 63, output devices are also included if the media.setsinkid.enabled preference is enabled.
Disabled From version 63: this feature is behind the media.setsinkid.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Full support 39
IE No support NoOpera Full support 34Safari Full support 11WebView Android Full support 47Chrome Android Full support 47Firefox Android Full support 63
Notes Disabled
Full support 63
Notes Disabled
Notes Prior to Firefox 63, enumerateDevices() only returned input devices. Starting with Firefox 63, output devices are also included if the media.setsinkid.enabled preference is enabled.
Disabled From version 63: this feature is behind the media.setsinkid.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Full support 39
Opera Android Full support 34Safari iOS Full support 11Samsung Internet Android Full support 5.0

Legend

Full support
Full support
No support
No support
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

See also