files/en-us/web/api/mediadevices/index.md
{{APIRef("Media Capture and Streams")}}{{SecureContext_Header}}
The MediaDevices interface of the {{domxref("Media Capture and Streams API", "", "", "nocode")}} 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.
{{InheritanceDiagram}}
Inherits properties from its parent interface, {{domxref("EventTarget")}}.
Inherits methods from its parent interface, {{domxref("EventTarget")}}.
MediaStream.// 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}}
{{Compat}}
MediaDevices object that can be used to access devices.MediaDevices and the MediaStream Recording APIMediaDevices and the MediaStream Recording API for video recording