我在使用HTML5测试我的网络摄像头时遇到了问题。
那是代码:
<meta charset="utf-8">
<title>Display Webcam Stream</title>
<script language="Javascript">
var video = document.getElementById('video');
if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true
}).then(function(stream) {
video.src = window.URL.createObjectURL(stream);
video.play();
});
}
</script>
在体内有:
<video id="video" autoplay="true"></video>
去网站(HTTPS)它问我网络摄像头的权限,我允许它打开网络摄像头,但看着控制台appairs一个错误:
testing.php:14 Uncaught (in promise) TypeError: Cannot set property 'src' of null
at testing.php:14
at <anonymous>
我该如何解决?
(抱歉英文不好)
谢谢。
没有id为video
的元素
如果有的话,getElementById
仍将返回null
。您需要在页面末尾或DOMContentLoaded
事件内移动脚本。