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 interface, EventTarget.



Fired when a media input or output device is attached to or removed from the user's computer. Also available via the ondevicechange property.


Inherits methods from its parent interface, EventTarget.


Obtains an array of information about the media input and output devices available on the system.


Returns an object conforming to MediaTrackSupportedConstraints indicating which constrainable properties are supported on the MediaStreamTrack interface. See Capabilities and constraints in Media Capture and Streams API (Media Stream) to learn more about constraints and how to use them.


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 a MediaStream.


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.


Prompts the user to select a specific audio output device.


'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');

.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');
  }; = stream; // make variable available to browser console
  video.srcObject = stream;
.catch(function(error) {
  if ( === 'ConstraintNotSatisfiedError') {
    errorMsg('The resolution ' + + 'x' + + ' px is not supported by your device.');
  } else if ( === '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);

function errorMsg(msg, error) {
  errorElement.innerHTML += '<p>' + msg + '</p>';
  if (typeof error !== 'undefined') {


Media Capture and Streams
# mediadevices

Browser compatibility

BCD tables only load in the browser

See also