--- title: MediaDevices slug: Web/API/MediaDevices tags: - API - Devices - Interface - Media - Media Capture and Streams API - Media Streams API - MediaDevices - NeedsTranslation - Reference - TopicStub - WebRTC translation_of: Web/API/MediaDevices ---
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 {{domxref("EventTarget")}}.
MediaDevices
object when a media input or output device is attached to or removed from the user's computer.Inherits methods from its parent {{domxref("EventTarget")}}.
'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 |
---|---|---|
{{SpecName('Media Capture', '#mediadevices', 'MediaDevices')}} | {{Spec2('Media Capture')}} | Initial definition |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | {{CompatChrome(47)}} | {{CompatVersionUnknown}} | {{CompatGeckoDesktop("36.0")}} | {{CompatNo}} | {{CompatOpera(30)}} | {{CompatNo}} |
enumerateDevices() |
{{CompatChrome(51)}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatNo}} | {{CompatOpera(38)}} | {{CompatNo}} |
getSupportedConstraints() |
{{CompatChrome(53)}} | {{CompatUnknown}} | {{CompatGeckoDesktop(44)}} | {{CompatNo}} | {{CompatOpera(40)}} | {{CompatNo}} |
ondevicechange and devicechange events |
{{CompatVersionUnknown}} | {{CompatUnknown}} | {{CompatGeckoDesktop(51)}}[1] | {{CompatNo}} | {{CompatVersionUnknown}} | {{CompatNo}} |
Stereo audio capture | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatGeckoDesktop(55)}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatNo}} |
Feature | Android Webview | Chrome for Android | Edge | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|---|
Basic support | {{CompatChrome(47)}} | {{CompatChrome(47)}} | {{CompatVersionUnknown}} | {{CompatGeckoMobile("36.0")}} | 2.2 | {{CompatNo}} | {{CompatOperaMobile(30)}} | {{CompatNo}} | {{CompatNo}} |
enumerateDevices() |
{{CompatChrome(51)}} | {{CompatChrome(51)}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatNo}} | {{CompatOperaMobile(38)}} | {{CompatNo}} | {{CompatNo}} |
getSupportedConstraints() |
{{CompatChrome(53)}} | {{CompatChrome(52)}} | {{CompatUnknown}} | {{CompatGeckoMobile(50)}} | {{CompatUnknown}} | {{CompatNo}} | {{CompatOperaMobile(40)}} | {{CompatNo}} | {{CompatNo}} |
ondevicechange and devicechange events |
{{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatUnknown}} | {{CompatGeckoMobile(51)}}[1] | {{CompatUnknown}} | {{CompatNo}} | {{CompatVersionUnknown}} | {{CompatNo}} | {{CompatNo}} |
Stereo audio capture | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatNo}} | {{CompatNo}} |
[1] Support for the devicechange
event and for {{domxref("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.
MediaDevices
object that can be used to access devices.