我正在學習使用getUserMedia()訪問javascript中的攝像頭。但我得到一個錯誤 - Not allowed to load local resource: blob:null/3485f5b8-46c1-4a40-946a-8de2588720f0
無法訪問攝像頭(Javascript)?
我在網上搜索,但他們說,我需要一個HTTPS連接或什麼,文件URL不允許,但我沒有完全明白髮生了什麼事情。
代碼 -
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="stuff, to, help, search, engines, not" name="keywords">
<meta content="What this page is about." name="description">
<meta content="Display Webcam Stream" name="title">
<title>Display Webcam Stream</title>
<style>
#container {
margin: 0px auto;
width: 500px;
height: 375px;
border: 10px #333 solid;
}
#videoElement {
width: 500px;
height: 375px;
background-color: #666;
}
</style>
</head>
<body>
<div id="container">
<video autoplay="true" id="videoElement">
</video>
</div>
<script>
var video = document.querySelector("#videoElement");
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.oGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia({video: true}, handleVideo, videoError);
}
function handleVideo(stream) {
video.src = window.URL.createObjectURL(stream);
}
function videoError(e) {
// do something
\t console.log("error");
}
</script>
</body>
</html>
提示允許網絡攝像頭在'file:'協議和firefox的stacksnippets – guest271314