gpt4 book ai didi

javascript - Chrome - 允许访问网络摄像头

转载 作者:行者123 更新时间:2023-11-30 05:33:09 25 4
gpt4 key购买 nike

我正在我的网站上使用 javascript,并且有一个涉及网络摄像头的想法。我的首要任务是简单地从用户的网络摄像头中获取数据并将其显示在网站上。

我的网站是hheim.com .这是所有代码:

index.html

<html>
<head>
<title>Selfie</title>
</head>
<body>
<video></video>
<script src="video.js"></script>
</body>
</html>

视频.js

(function () {
navigator.getMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia);
navigator.getMedia(
{video:true, audio:false},
function (mediaStream) {
var video = document.getElemtsByTagName('video')[0];
video.src = window.URL.createObjectURL(mediaStream);
video.play();
},
function (error) {
console.log(error);
})
})();

当我加载页面时,Chrome 说它正在阻止网络摄像头。好的,我预料到了。按预期工作。但是,当我单击摄像头图标为我的站点取消阻止时,仅有的两个选项是“询问”和“继续阻止”。它默认为“继续阻止”。

当我更改为“询问”时,会下拉一个工具栏,提示我重新加载页面。当我重新加载页面时,我没有被询问,它仍然被阻止,并且仍然默认为“继续阻止”。

我已经进行了一些谷歌搜索,但似乎无法找到解决方案。我需要包含更多代码吗?我是这里有问题的人吗(cookie 等)?或者这是 Chrome 的实际问题?

最佳答案

首先,您必须检查用户媒体是否可用并准备好被授予。记住 eval 是邪恶的 :)

navigator.getUserMedia = ( navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);

if (navigator.getUserMedia) {
navigator.getUserMedia (

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

// successCallback
function(localMediaStream) {
var video = document.getElementById('stream');
video.src = window.URL.createObjectURL(localMediaStream);
// Do something with the video here, e.g. video.play()
},

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

https://developer.mozilla.org/en-US/docs/NavigatorUserMedia.getUserMedia

设置视频id和自动播放属性;

<video id="stream" autoplay></video>

关于javascript - Chrome - 允许访问网络摄像头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25634102/

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