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.
var enumeratorPromise = navigator.mediaDevices.enumerateDevices();
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.
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 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=
Specification | Status | Comment |
---|---|---|
Media Capture and Streams The definition of 'mediaDevices: enumerateDevices' in that specification. | Candidate Recommendation | Initial definition. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 47 | Yes | 63
|
No | 34 | 11 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 47 | 47 | Yes | 39 | 34 | 11 | ? |
navigator.mediaDevices.getUserMedia()
getUserMedia()
for taking photos rather than video.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices