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.
Inherits properties from its parent EventTarget
.
MediaDevices.ondevicechange
devicechange
event. This event is delivered to the MediaDevices
object when a media input or output device is attached to or removed from the user's computer.Inherits methods from its parent EventTarget
.
EventTarget.addEventListener()
MediaDevices.enumerateDevices()
MediaDevices.getSupportedConstraints()
MediaTrackSupportedConstraints
indicating which constrainable properties are supported on the MediaStreamTrack
interface. See Capabilities and constraints in Media Capture and Streams API (Media Streams) to learn more about constraints and how to use them.MediaDevices.getUserMedia()
MediaStream
containing a video track and/or an audio track with the input.EventTarget.removeEventListener()
'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.onremovetrack = 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); } }
Specification | Status | Comment |
---|---|---|
Media Capture and Streams The definition of 'MediaDevices' in that specification. | Candidate Recommendation | Initial definition |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 47 | Yes | 36 | No | 30 | No |
ondevicechange |
57 | ? | 52
|
? | 34 | ? |
enumerateDevices |
47 | Yes | 63
|
No | 34 | No |
getSupportedConstraints |
53 | ? | 44 | ? | 40 | 11 |
getUserMedia |
52
|
Yes | 36
|
No | 40
|
11 |
Stereo audio capture | ? | ? | 55 | No | ? | No |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 47 | 47 | Yes | 36 | 30 | No | ? |
ondevicechange |
No | No | ? | ? | 34 | ? | ? |
enumerateDevices |
47 | 47 | Yes | 39 | 34 | No | ? |
getSupportedConstraints |
53 | 52 | ? | 50 | 40 | 11 | ? |
getUserMedia |
53 | 52
|
Yes | 36
|
40
|
11 | ? |
Stereo audio capture | ? | ? | ? | No | ? | No | ? |
Navigator.mediaDevices
: Returns a reference to a MediaDevices
object that can be used to access devices.
© 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