Baseline Widely available *
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.
Inherits properties from its parent interface, EventTarget
.
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.
devicechange
Fired when a media input or output device is attached to or removed from the user's computer.
// 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 See also
getDisplayMedia()
method.Navigator.mediaDevices
: Returns a reference to a MediaDevices
object that can be used to access devices.MediaDevices
and the MediaStream Recording APIMediaDevices
and the MediaStream Recording API for video recordingRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4