我正在尝试使用 访问用户的网络摄像头navigator.getUserMedia()。我将 video.srcObject 分配给该流。但我的视频出现黑屏。我尝试过事件 navigator.mediaDevices.getUserMedia()
<video controls id="webcam"></video>
<script>
const webcam = document.getElementById("webcam");
function startVideo() {
navigator
.getUserMedia({
video: true,
audio: false
},
liveStream => {
console.log(liveStream);
webcam.setAttribute("controls", 'true');
webcam.srcObject = liveStream;
webcam.play();
},
error => console.log(error)
)
}
startVideo();
</script>
Run Code Online (Sandbox Code Playgroud)