enumerateDevices() before getUserMedia()

This commit is contained in:
Nebel 2023-09-29 12:07:00 +09:00
parent d785ed14a1
commit 7ffb578066
Signed by: nebel
GPG key ID: 79807D08C6EF6460

View file

@ -9,6 +9,24 @@
<div id="player"></div>
<div id="devices"></div>
<script type="module">
const deviceId = location.hash.slice(1);
const playerElement = document.getElementById("player");
const videoElement = document.createElement("video");
const srcObject = await navigator.mediaDevices.getUserMedia({
video: {
deviceId,
},
});
Object.assign(videoElement, {
controls: "controls",
autoplay: true,
muted: true,
srcObject,
});
playerElement.append(videoElement);
const devicesElement = document.getElementById("devices");
const devices = (await navigator.mediaDevices.enumerateDevices()).filter(
({ kind }) => kind === "videoinput",
@ -27,24 +45,6 @@
}
window.onhashchange = () => location.reload();
const deviceId = location.hash.slice(1);
const playerElement = document.getElementById("player");
const videoElement = document.createElement("video");
const srcObject = await navigator.mediaDevices.getUserMedia({
video: {
deviceId,
},
});
Object.assign(videoElement, {
controls: "controls",
autoplay: true,
muted: true,
srcObject,
});
playerElement.append(videoElement);
</script>
</body>
</html>