gpt4 book ai didi

javascript - html5 getUserMedia Api 停止网络摄像头按钮不起作用

转载 作者:行者123 更新时间:2023-11-28 00:01:05 25 4
gpt4 key购买 nike

我正在本地主机 XAMPP 服务器上执行此代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<video onclick="changeFilter(this);"width=200 height=200 id="video" controls autoplay></video>
<p>
<button onclick="startWebcam();">Start WebCam</button>
<button onclick="stopWebcam();">Stop WebCam</button>
</p>
<script>
navigator.getUserMedia = ( navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);

var webcamStream;

function startWebcam() {
if (navigator.getUserMedia) {
console.log("toto");
navigator.getUserMedia (

// constraints
{
video: true,
audio: false
},

// successCallback
function(localMediaStream) {
var video = document.querySelector('video');
video.src = window.URL.createObjectURL(localMediaStream);
webcamStream = localMediaStream;
},

// errorCallback
function(err) {
console.log("The following error occured: " + err);
}
);
} else {
console.log("getUserMedia not supported");
}
}

function stopWebcam() {
localMediaStream.stop();
}
</script>
</body>
</html>

此代码启动我的网络摄像头,但是当我按下停止网络摄像头按钮时,控制台会出现以下错误:

Uncaught TypeError: Cannot read property 'stop' of undefined function stopWebcam() { webcamStream.stop(); }

我是 JavaScript 新手,我在这里看不到这个问题。

最佳答案

localMediaStream 在 stopWebcam() 中不可用。查看这篇文章以了解有关 What is the scope of variables in JavaScript? 的更多信息

尝试...

  function stopWebcam() {
webcamStream.stop();
}

关于javascript - html5 getUserMedia Api 停止网络摄像头按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31799283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com