--- 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 ---
{{APIRef("Media Capture and Streams")}}{{SeeCompatTable}}

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 {{domxref("EventTarget")}}.

Event handlers

{{ domxref("MediaDevices.ondevicechange") }}
The event handler for the {{event("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.

Methods

Inherits methods from its parent {{domxref("EventTarget")}}.

{{ domxref("EventTarget.addEventListener()") }}
Registers an event handler to a specific event type.
{{ domxref("MediaDevices.enumerateDevices()") }}
Obtains an array of information about the media input and output devices available on the system.
{{domxref("MediaDevices.getSupportedConstraints()")}}
Returns an object conforming to {{domxref("MediaTrackSupportedConstraints")}} indicating which constrainable properties are supported on the {{domxref("MediaStreamTrack")}} interface. See {{SectionOnPage("/en-US/docs/Web/API/Media_Streams_API", "Capabilities and constraints")}} to learn more about constraints and how to use them.
{{ domxref("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 {{domxref("MediaStream")}} containing a video track and/or an audio track with the input.
{{ domxref("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.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);
  }
}

Specifications

Specification Status Comment
{{SpecName('Media Capture', '#mediadevices', 'MediaDevices')}} {{Spec2('Media Capture')}} Initial definition

Browser compatibility

{{CompatibilityTable}}
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.

See also