W3cubDocs

/Web APIs

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.

EventTarget MediaDevices

Instance properties

Inherits properties from its parent interface, EventTarget.

Instance 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 the MediaStreamTrack interface. See Media Streams API 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 a MediaStream.

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.

selectAudioOutput() Experimental

Prompts the user to select a specific audio output device.

Events

devicechange

Fired when a media input or output device is attached to or removed from the user's computer.

Example

js

// Put variables in global scope to make them available to the browser console.
const video = document.querySelector("video");
const constraints = {
  audio: false,
  video: true,
};

navigator.mediaDevices
  .getUserMedia(constraints)
  .then((stream) => {
    const videoTracks = stream.getVideoTracks();
    console.log("Got stream with constraints:", constraints);
    console.log(`Using video device: ${videoTracks[0].label}`);
    stream.onremovetrack = () => {
      console.log("Stream ended");
    };
    video.srcObject = stream;
  })
  .catch((error) => {
    if (error.name === "OverconstrainedError") {
      console.error(
        `The resolution ${constraints.video.width.exact}x${constraints.video.height.exact} px is not supported by your device.`,
      );
    } else if (error.name === "NotAllowedError") {
      console.error(
        "You need to grant this page permission to access your camera and microphone.",
      );
    } else {
      console.error(`getUserMedia error: ${error.name}`, error);
    }
  });

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
MediaDevices 47 12 33 No 30 11 47 47 36 30 11 5.0
devicechange_event 57 12 52 No 34 11 No No 52 43 11 No
enumerateDevices 47 12
116enumerateDevices() enumerates both input and output devices. Previously only input devices were returned.
39enumerateDevices() only returns input devices.
No 34 11 47 47
116enumerateDevices() enumerates both input and output devices. Previously only input devices were returned.
39enumerateDevices() only returns input devices.
34 11 5.0
getDisplayMedia 72 79
17–79Available as a member of Navigator instead of MediaDevices.
66
33–66Since Firefox 33 you can capture screen data using getUserMedia(), with a video constraint called mediaSource. Before 52 it relied on a client-configurable list of allowed sites.
No 60 13
NoFrom Chrome Android 72 to 88, this method was exposed, but always failed with NotAllowedError. See bug 487935.
NoFrom Chrome Android 72 to 88, this method was exposed, but always failed with NotAllowedError. See bug 487935.
NoFrom Firefox Android 66 to 79, this method was exposed, but always failed with NotAllowedError.
NoFrom Opera Android 72 to 88, this method was exposed, but always failed with NotAllowedError. See bug 487935.
No
NoFrom Samsung Internet Android 72 to 88, this method was exposed, but always failed with NotAllowedError. See bug 487935.
getSupportedConstraints 53 12 44 No 40 11 53 52 50 41 11 6.0
getUserMedia
53If you need this capability before version 53, refer to navigator.webkitGetUserMedia, a prefixed form of the deprecated navigator.getUserMedia API.
12
36["If you need this capability before version 36, refer to navigator.mozGetUserMedia, a prefixed form of the deprecated navigator.getUserMedia API.", "Before Firefox 55, getUserMedia() incorrectly returns NotSupportedError when the list of constraints specified is empty, or has all constraints set to false. Starting in Firefox 55, this situation now correctly calls the failure handler with a TypeError.", "When using the Firefox-specific video constraint called mediaSource to request display capture, Firefox 66 and later consider values of screen and window to both cause a list of screens and windows to be shown.", "Starting in Firefox 66, getUserMedia() can no longer be used in sandboxed <iframe>s or data URLs entered in the address bar by the user."]
No
40If you need this capability before version 40, refer to navigator.webkitGetUserMedia, a prefixed form of the deprecated navigator.getUserMedia API.
11 53
53If you need this capability before version 53, refer to navigator.webkitGetUserMedia, a prefixed form of the deprecated navigator.getUserMedia API.
36["If you need this capability before version 36, refer to navigator.mozGetUserMedia, a prefixed form of the deprecated navigator.getUserMedia API.", "Before Firefox 55, getUserMedia() incorrectly returns NotSupportedError when the list of constraints specified is empty, or has all constraints set to false. Starting in Firefox 55, this situation now correctly calls the failure handler with a TypeError.", "When using the Firefox-specific video constraint called mediaSource to request display capture, Firefox 66 and later consider values of screen and window to both cause a list of screens and windows to be shown.", "Starting in Firefox 66, getUserMedia() can no longer be used in sandboxed <iframe>s or data URLs entered in the address bar by the user."]
41If you need this capability before version 41, refer to navigator.webkitGetUserMedia, a prefixed form of the deprecated navigator.getUserMedia API.
11 6.0
selectAudioOutput No No 116 No No No No No No No No No
setCaptureHandleConfig 102 102 No No 88 No No No No No No No

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices