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 interface, EventTarget
.
Events
devicechange
- Fired when a media input or output device is attached to or removed from the user's computer. Also available via the
ondevicechange
property.
Methods
Inherits methods from its parent interface, EventTarget
.
enumerateDevices()
- Obtains an array of information about the media input and output devices available on the system.
getSupportedConstraints()
- Returns an object conforming to
MediaTrackSupportedConstraints
indicating which constrainable properties are supported on theMediaStreamTrack
interface. See Capabilities and constraints in Media Capture and Streams API (Media Stream) to learn more about constraints and how to use them. getDisplayMedia()
- Prompts the user to select a display or portion of a display (such as a window) to capture as a
MediaStream
for sharing or recording purposes. Returns a promise that resolves to aMediaStream
. getUserMedia()
- With the user's permission through a prompt, turns on a camera and/or a microphone on the system and provides a
MediaStream
containing a video track and/or an audio track with the input.
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.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.height.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 StreamsThe definition of 'MediaDevices' in that specification. | Candidate Recommendation | Initial definition |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
MediaDevices
|
Chrome
Full support 47 |
Edge
Full support 12 |
Firefox
Full support 33 |
IE
No support No |
Opera
Full support 30 |
Safari
Full support 11 |
WebView Android
Full support 47 |
Chrome Android
Full support 47 |
Firefox Android
Full support 36 |
Opera Android
Full support 30 |
Safari iOS
Full support 11 |
Samsung Internet Android
Full support 5.0 |
devicechange event
|
Chrome
Full support 57 |
Edge
Full support 12 |
Firefox
Full support 52 |
IE
No support No |
Opera
Full support 34 |
Safari
No support No |
WebView Android
No support No |
Chrome Android
No support No |
Firefox Android
? |
Opera Android
Full support 43 |
Safari iOS
No support No |
Samsung Internet Android
No support No |
enumerateDevices
|
Chrome
Full support 47 |
Edge
Full support 12 |
Firefox Full support 63 Full support 63 Notes' Prior to Firefox 63, |
IE
No support No |
Opera
Full support 34 |
Safari
Full support 11 |
WebView Android
Full support 47 |
Chrome Android
Full support 47 |
Firefox Android Full support 63 Full support 63 Notes' Prior to Firefox 63, |
Opera Android
Full support 34 |
Safari iOS
Full support 11 |
Samsung Internet Android
Full support 5.0 |
getDisplayMedia()
|
Chrome Full support 72 Full support 72 No support 70 — 72 Notes' Available as a member of |
Edge Full support 79 Full support 79 Full support 17 Notes' Available as a member of |
Firefox Full support 66 Full support 66 No support 33 — 66 Notes' Since Firefox 33 you can capture screen data using |
IE
No support No |
Opera Full support 60 Full support 60 No support 57 — 60 Notes' Available as a member of |
Safari
Full support 13 |
WebView Android No support No No support No Notes' API is available, but will always fail with |
Chrome Android
No support No |
Firefox Android No support No No support No Notes' API is available, but will always fail with |
Opera Android
No support No |
Safari iOS
No support No |
Samsung Internet Android
No support No |
getSupportedConstraints
|
Chrome
Full support 53 |
Edge
Full support 12 |
Firefox
Full support 44 |
IE
No support No |
Opera
Full support 40 |
Safari
Full support 11 |
WebView Android
Full support 53 |
Chrome Android
Full support 52 |
Firefox Android
Full support 50 |
Opera Android
Full support 41 |
Safari iOS
Full support 11 |
Samsung Internet Android
Full support 6.0 |
getUserMedia
|
Chrome Full support 53 Full support 53 Notes' If you need this capability before version 53, refer to |
Edge
Full support 12 |
Firefox Full support 36 Full support 36 Notes' If you need this capability before version 36, refer to |
IE
No support No |
Opera Full support 40 Full support 40 Notes' If you need this capability before version 40, refer to |
Safari
Full support 11 |
WebView Android
Full support 53 |
Chrome Android Full support 53 Full support 53 Notes' If you need this capability before version 53, refer to |
Firefox Android Full support 36 Full support 36 Notes' If you need this capability before version 36, refer to |
Opera Android Full support 41 Full support 41 Notes' If you need this capability before version 41, refer to |
Safari iOS
Full support 11 |
Samsung Internet Android
Full support 6.0 |
ondevicechange
|
Chrome
Full support 57 |
Edge
Full support 12 |
Firefox Full support 52 Full support 52 No support 51 — 52 Notes' |
IE
No support No |
Opera
Full support 34 |
Safari
Full support 11 |
WebView Android
No support No |
Chrome Android
No support No |
Firefox Android
Full support Yes |
Opera Android
Full support 34 |
Safari iOS
Full support 11 |
Samsung Internet Android
No support No |
Stereo audio capture | Chrome
? |
Edge
? |
Firefox
Full support 55 |
IE
No support No |
Opera
? |
Safari
No support No |
WebView Android
? |
Chrome Android
? |
Firefox Android
No support No |
Opera Android
? |
Safari iOS
No support No |
Samsung Internet Android
? |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.'
- See implementation notes.
- User must explicitly enable this feature.'
- User must explicitly enable this feature.
See also
- Media Capture and Streams API: The API this interface is part of.
- Screen Capture API: The API defining the
getDisplayMedia()
method. - 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) - OpenLang: HTML5 video language lab web application using
MediaDevices
and the MediaStream Recording API for video recording (source on GitHub)
MediaDevices by Mozilla Contributors is licensed under CC-BY-SA 2.5.