Перевод не завершен. Пожалуйста, помогите перевести эту статью с английского.
Это экспериментальная технология
Так как спецификация этой технологии ещё не стабилизировалась, смотрите таблицу совместимости по поводу использования в различных браузерах. Также заметьте, что синтаксис и поведение экспериментальной технологии может измениться в будущих версиях браузеров, вслед за изменениями спецификации.
The MediaDevices
interface provides access to connected media input devices like cameras and microphones, as well as screen sharing. In essence, it lets you obtain access to any hardware source of media data.
Properties
Inherits properties from its parent EventTarget
.
Event handlers
MediaDevices.ondevicechange
- The event handler for the
devicechange
event. This event is delivered to theMediaDevices
object when a media input or output device is attached to or removed from the user's computer.
Methods
Inherits methods from its parent EventTarget
.
EventTarget.addEventListener()
- Registers an event handler to a specific event type.
MediaDevices.enumerateDevices()
- Obtains an array of information about the media input and output devices available on the system.
MediaDevices.getSupportedConstraints()
- Returns an object conforming to
MediaTrackSupportedConstraints
indicating which constrainable properties are supported on theMediaStreamTrack
interface. See Capabilities and constraints в Media Capture and Streams API (Media Stream) to learn more about constraints and how to use them. MediaDevices.getUserMedia()
- With the user's permission through a prompt, turns on a camera or screensharing and/or a microphone on the system and provides a
MediaStream
containing a video track and/or an audio track with the input. EventTarget.removeEventListener()
- Removes an event listener.
Example
'use strict'; // Put variables in global scope to make them available to the browser console. var video = document.querySelector('video'); var constraints = window.constraints = { audio: false, video: true }; var errorElement = document.querySelector('#errorMsg'); navigator.mediaDevices.getUserMedia(constraints) .then(function(stream) { var videoTracks = stream.getVideoTracks(); console.log('Got stream with constraints:', constraints); console.log('Using video device: ' + videoTracks[0].label); stream.onended = function() { console.log('Stream ended'); }; window.stream = stream; // make variable available to browser console video.srcObject = stream; }) .catch(function(error) { if (error.name === 'ConstraintNotSatisfiedError') { errorMsg('The resolution ' + constraints.video.width.exact + 'x' + constraints.video.width.exact + ' px is not supported by your device.'); } else if (error.name === 'PermissionDeniedError') { errorMsg('Permissions have not been granted to use your camera and ' + 'microphone, you need to allow the page access to your devices in ' + 'order for the demo to work.'); } errorMsg('getUserMedia error: ' + error.name, error); }); function errorMsg(msg, error) { errorElement.innerHTML += '<p>' + msg + '</p>'; if (typeof error !== 'undefined') { console.error(error); } }
Specifications
Specification | Status | Comment |
---|---|---|
Media Capture and Streams Определение 'MediaDevices' в этой спецификации. |
Кандидат в рекомендации | Initial definition |
Browser compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Microsoft Edge | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|---|
Basic support | (Да) | (Да) | 36.0 (36.0) | Нет | ? | Нет | Нет |
enumerateDevices() |
51.0 | ? | ? | Нет | ? | Нет | Нет |
getSupportedConstraints() |
(Да) | ? | 50 (50) | Нет | ? | Нет | Нет |
ondevicechange and devicechange events |
(Да) | ? | 51 (51)[1] | Нет | ? | Нет | Нет |
Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|---|
Basic support | Нет | Нет | (Да) | 36.0 (36.0) | 2.2 | Нет | Нет | Нет | Нет |
enumerateDevices() |
Нет | Нет | ? | ? | ? | Нет | Нет | Нет | Нет |
getSupportedConstraints() |
Нет | Нет | ? | 50.0 (50) | ? | Нет | Нет | Нет | Нет |
[1] Support for the devicechange
event and for MediaDevices.ondevicechange
landed in Firefox 51, but only for Mac, and disabled by default. It can be enabled by setting the preference media.ondevicechange.enabled
to true
. Support for this event was added for Linux and Windows—and it was enabled by default—starting in Firefox 52.
See also
- Media Capture and Streams API: The API this interface is part of.
- WebRTC API
Navigator.mediaDevices
: Returns a reference to aMediaDevices
object that can be used to access devices.- CameraCaptureJS: HTML5 video capture and playback using MediaDevices and the MediaStream Recording API (source on GitHub)