JS, navigator.mediaDevices.getUserMedia() not working



我正在尝试从客户端的网络摄像头获取视频。我编写了代码,没有出现任何错误。然而,当我尝试获取视频时,(在Google Chrome中(什么都没有显示,或者(在Mozilla Firefox中(只显示一帧视频。我以前测试过,效果很好,但现在,我不知道为什么,它不起作用。我找了一下,什么也没找到。任何帮助我都非常感激。感谢

这是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
html, body {
height: 100%;
}
body {
margin: 0;
}
video {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
z-index: -1;
}
</style>
</head>
<body>
<button id="shareBtn">Share Video</button>
<script>
async function startCapture(displayMediaOptions) {
let stream = null;
try {
stream = await navigator.mediaDevices.getUserMedia(displayMediaOptions);
video = document.createElement("video");
video.srcObject = stream;
document.body.appendChild(video);
} catch(err) {
console.error(err);
}
}
document.getElementById("shareBtn").addEventListener("click", () => {
startCapture({ video:true });
});
</script>
</body>
</html>

您的代码正在工作,但视频已暂停。在代码中添加1行:

<script>
async function startCapture(displayMediaOptions) {
let stream = null;
try {
stream = await navigator.mediaDevices.getUserMedia(displayMediaOptions);
video = document.createElement("video");
video.srcObject = stream;
video.setAttribute('autoplay', true); /* THIS */
document.body.appendChild(video);
} catch(err) {
console.error(err);
}
}
document.getElementById("shareBtn").addEventListener("click", () => {
startCapture({ video:true });
});
</script>

相关内容

  • 没有找到相关文章

最新更新