MediaDevices
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2017.
* Some parts of this feature may have varying levels of support.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The MediaDevices interface of the Media Capture and Streams API 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.
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
MediaTrackSupportedConstraintsindicating which constrainable properties are supported on theMediaStreamTrackinterface. 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
MediaStreamfor 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
MediaStreamcontaining a video track and/or an audio track with the input. selectAudioOutput()-
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
// 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
| Specification |
|---|
| Media Capture and Streams # mediadevices |
Browser compatibility
| desktop | mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
MediaDevices
|
|
|
|
|
|
|
|
|
|
|
|
|
devicechange event
|
|
|
|
|
|
|
|
|
|
|
|
|
enumerateDevices
|
|
|
|
|
|
|
|
|
|
|
|
|
getDisplayMedia()
|
|
|
|
|
|
|
|
|
|
|
|
|
| Audio capture support |
|
|
|
|
|
|
|
|
|
|
|
|
controller option
|
|
|
|
|
|
|
|
|
|
|
|
|
monitorTypeSurfaces option
|
|
|
|
|
|
|
|
|
|
|
|
|
preferCurrentTab option
|
|
|
|
|
|
|
|
|
|
|
|
|
selfBrowserSurface option
|
|
|
|
|
|
|
|
|
|
|
|
|
surfaceSwitching option
|
|
|
|
|
|
|
|
|
|
|
|
|
systemAudio option
|
|
|
|
|
|
|
|
|
|
|
|
|
windowAudio option
|
|
|
|
|
|
|
|
|
|
|
|
|
getSupportedConstraints
|
|
|
|
|
|
|
|
|
|
|
|
|
aspectRatio property in returned object
|
|
|
|
|
|
|
|
|
|
|
|
|
autoGainControl property in returned object
|
|
|
|
|
|
|
|
|
|
|
|
|
channelCount property in returned object
|
|
|
|
|
|
|
|
|
|
|
|
|
deviceId property in returned object
|
|
|
|
|
|
|
|
|
|
|
|
|
echoCancellation property in returned object
|
|
|
|
|
|
|
|
|
|
|
|
|
facingMode property in returned object
|
|
|
|
|
|
|
|
|
|
|
|
|
frameRate property in returned object
|
|
|
|
|
|
|
|
|
|
|
|
|
groupId property in returned object
|
|
|
|
|
|
|
|
|
|
|
|
|
height property in returned object
|
|
|
|
|
|
|
|
|
|
|
|
|
latency property in returned object
|
|
|
|
|
|
|
|
|
|
|
|
|
noiseSuppression property in returned object
|
|
|
|
|
|
|
|
|
|
|
|
|
resizeMode property in returned object
|
|
|
|
|
|
|
|
|
|
|
|
|
sampleRate property in returned object
|
|
|
|
|
|
|
|
|
|
|
|
|
sampleSize property in returned object
|
|
|
|
|
|
|
|
|
|
|
|
|
width property in returned object
|
|
|
|
|
|
|
|
|
|
|
|
|
getUserMedia
|
|
|
|
|
|
|
|
|
|
|
|
|
constraints.resizeMode parameter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Secure context required
|
|
|
|
|
|
|
|
|
|
|
|
|
selectAudioOutput
|
|
|
|
|
|
|
|
|
|
|
|
|
setCaptureHandleConfig
|
|
|
|
|
|
|
|
|
|
|
|
|
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- Partial support
- Partial support
- No support
- No support
- Experimental. Expect behavior to change in the future.
- Non-standard. Check cross-browser support before using.
- See implementation notes.
- Has more compatibility info.
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 aMediaDevicesobject that can be used to access devices.- CameraCaptureJS: HTML video capture and playback using
MediaDevicesand the MediaStream Recording API - OpenLang: HTML video language lab web application using
MediaDevicesand the MediaStream Recording API for video recording