Это экспериментальная технология
Так как спецификация этой технологии ещё не стабилизировалась, смотрите таблицу совместимости по поводу использования в различных браузерах. Также заметьте, что синтаксис и поведение экспериментальной технологии может измениться в будущих версиях браузеров, вслед за изменениями спецификации.
Метод MediaDevices.enumerateDevices()
собирает информацию о медиа-устройствах ввода и вывода, доступных в системе.
Syntax
navigator.mediaDevices.enumerateDevices() .then(function(MediaDeviceInfo) { ... })
Returns
Возвращает Promise
который, если выполнится успешно, вернёт массив экземпляров MediaDeviceInfo
, которые содержат информацию о доступных медиа-устройствах ввода и вывода.
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 Определение 'mediaDevices.enumerateDevices' в этой спецификации. |
Кандидат в рекомендации | Initial definition. |
Browser compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | 45.0 [1] | (Да) | 39 | Нет | Нет | Нет |
Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | Firefox OS (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|---|
Basic support | Нет | Нет | (Да) | 39 | 39 | Нет | Нет | Нет | Нет |
[1] Behind a flag.
Chrome and Opera compatibility
- This interface is available in Chrome and Opera through the adapter.js polyfill.
See also
- navigator.mediaDevices.getUserMedia
- WebRTC - the introductory page to the API
- MediaStream API - the API for the media stream objects
- Taking webcam photos - a tutorial on using
getUserMedia() for taking photos rather than video.