<iframe src="https://mycode.example.net/etc" allow="camera; microphone">
</iframe>MediaDevices: getUserMedia() method / notebook
MediaDevices: getUserMedia() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
async function getMedia(constraints) {
let stream = null;
try {
stream = await navigator.mediaDevices.getUserMedia(constraints);
/* use the stream */
} catch (err) {
/* handle the error */
}
}navigator.mediaDevices
.getUserMedia(constraints)
.then((stream) => {
/* use the stream */
})
.catch((err) => {
/* handle the error */
});getUserMedia({
audio: true,
video: true,
});getUserMedia({
audio: true,
video: { width: 1280, height: 720 },
});getUserMedia({
audio: true,
video: { width: 1280, height: 720, resizeMode: "crop-and-scale" },
});getUserMedia({
audio: true,
video: {
width: { min: 1280 },
height: { min: 720 },
},
});getUserMedia({
audio: true,
video: {
width: { min: 1024, ideal: 1280, max: 1920 },
height: { min: 576, ideal: 720, max: 1080 },
},
});getUserMedia({
audio: true,
video: {
width: { ideal: 1280 },
height: { ideal: 720 },
},
});getUserMedia({
audio: true,
video: { facingMode: "user" },
});getUserMedia({
audio: true,
video: {
facingMode: { exact: "environment" },
},
});getUserMedia({
video: {
deviceId: myPreferredCameraDeviceId,
},
});getUserMedia({
video: {
deviceId: {
exact: myExactCameraOrBustDeviceId,
},
},
});// Prefer camera resolution nearest to 1280x720.
const constraints = {
audio: true,
video: { width: 1280, height: 720 },
};
navigator.mediaDevices
.getUserMedia(constraints)
.then((mediaStream) => {
const video = document.querySelector("video");
video.srcObject = mediaStream;
video.onloadedmetadata = () => {
video.play();
};
})
.catch((err) => {
// always check for errors at the end.
console.error(`${err.name}: ${err.message}`);
});const constraints = {
video: { frameRate: { ideal: 10, max: 15 } },
};let front = false;
document.getElementById("flip-button").onclick = () => {
front = !front;
};
const constraints = {
video: { facingMode: front ? "user" : "environment" },
};