MediaDevices: enumerateDevices() method
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.
Access to particular devices is gated by the Permissions API. The list of returned devices will omit any devices for which the corresponding permission has not been granted, including: microphone
, camera
, speaker-selection
(for output devices), and so on.
Syntax
Parameters
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. The order is significant — the default capture devices will be listed first.
Only device types for which permission has been granted are "available". Also note that if a speaker-selection
Permissions Policy is used to block use of audio outputs, they won't be available in the list.
If enumeration fails, the promise is rejected.
Security requirements
Access to the API is subject to the following constraints:
Examples
Here's an example of using enumerateDevices()
. It outputs a list of the device IDs, with their labels if available.
if (!navigator.mediaDevices?.enumerateDevices) {
console.log("enumerateDevices() not supported.");
} else {
navigator.mediaDevices
.enumerateDevices()
.then((devices) => {
devices.forEach((device) => {
console.log(`${device.kind}: ${device.label} id = ${device.deviceId}`);
});
})
.catch((err) => {
console.error(`${err.name}: ${err.message}`);
});
}
This might produce:
videoinput: id = csO9c0YpAf274OuCPUA53CNE0YHlIr2yXCi+SqfBZZ8=
audioinput: id = RKxXByjnabbADGQNNZqLVLdmXlS0YkETYCIbg+XxnvM=
audioinput: id = r2/xw1xUPIyZunfV1lGrKOma5wTOvCkWfZ368XCndm0=
or if one or more MediaStream
s 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
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
enumerateDevices |
47 |
12 |
63Before Firefox 63, enumerateDevices() only returned input devices. Starting with Firefox 63, output devices are also included if the media.setsinkid.enabled preference is enabled.39enumerateDevices() only returns input devices.previewenumerateDevices() enumerates both input and output devices. Previously only input devices were returned.
|
No |
34 |
11 |
47 |
47 |
39enumerateDevices() only returns input devices. |
34 |
11 |
5.0 |
See also