[英]Chrome does not prompt user for camera and/or microphone access even in secure context
I have a webpage served via https
with the following script:我有一个通过
https
提供的网页,其中包含以下脚本:
const userMediaConstraints = {
video: true,
audio: true
};
navigator.mediaDevices.getUserMedia(userMediaConstraints)
.then(stream => {
console.log("Media stream captured.")
});
When I open it in Firefox or Safari, a prompt asking for permission to use camera and microphone appears.当我在 Firefox 或 Safari 中打开它时,会出现询问是否允许使用相机和麦克风的提示。 But not in Chrome or Opera.
但不在 Chrome 或 Opera 中。 There the access is blocked by default and I have to go to site settings and manually allow the access, which is set to Default(ask).
默认情况下,访问被阻止,我必须转到站点设置并手动允许访问,该访问设置为默认(询问)。
window.isSecureContext
is true
. window.isSecureContext
是true
。navigator.permissions.query({name:'camera'})
resolves to name: "video_capture", onchange: null, state: "prompt"
. navigator.permissions.query({name:'camera'})
解析为name: "video_capture", onchange: null, state: "prompt"
。 It looks like Chrome and Opera should show prompt, but they do not.看起来 Chrome 和 Opera 应该显示提示,但它们没有。 I tested it on a different machine with different user that had no prior history with the website with the same result.
我在不同用户的不同机器上对其进行了测试,这些用户之前没有使用该网站的历史记录,结果相同。
What could be wrong?有什么问题?
You've said you've tried Safari, which suggests to me you're on a Mac.您说过您尝试过 Safari,这表明您使用的是 Mac。
This issue on the Chrome issues list fits your description really well. Chrome 问题列表中的这个问题非常适合您的描述。 (I got there from this on the
webrtc/samples
GitHub project.) It comes down to a Mac OS security setting. (我是从webrtc
webrtc/samples
GitHub 项目中获得的。)它归结为 Mac OS 安全设置。 Quotingcomment #3 :引用评论#3 :
Please check in Mac system preferences > Security & Privacy > Privacy > Microphone, that Chrome is checked.
请在 Mac 系统偏好设置 > 安全和隐私 > 隐私 > 麦克风中检查 Chrome 是否已检查。
(In your case since you want video, you'd also probably have to tick the box under Security & Privacy > Privacy > Camera or similar.) (在您的情况下,由于您想要视频,您可能还必须勾选安全和隐私>隐私>相机或类似内容下的框。)
It turns out, having video with autoplay attribute in HTML or similar lines on page load in Javascript prevent Chrome from showing the prompt.事实证明,在 HTML 中具有自动播放属性的视频或在 Javascript 中页面加载的类似行会阻止 Chrome 显示提示。
HTML that causes this problem:导致此问题的 HTML:
<video autoplay="true"></video>
Javascript that causes this problem:导致此问题的 Javascript:
localVideo = document.createElement('video');
videoContainer.append(localVideo);
localVideo.setAttribute('id','localVideo');
localVideo.play();
My guess is that the issue has to do with Chrome autoplay policy .我的猜测是这个问题与Chrome 自动播放策略有关。 Perhaps, Chrome treats my website as providing bad user experience and blocks the prompt?
也许,Chrome 将我的网站视为提供糟糕的用户体验并阻止提示?
I removed <video>
from HTML and altered Javascript to create a relevant DOM on getUserMedia
:我从 HTML 中删除了
<video>
并更改了 Javascript 以在getUserMedia
上创建相关的 DOM:
let localStream = new MediaStream();
let localAudioTrack;
let localVideoTrack;
let localVideo;
const userMediaConstraints = {
video: true,
audio: true
};
navigator.mediaDevices.getUserMedia(userMediaConstraints)
.then(stream => {
localAudioTrack = stream.getAudioTracks()[0];
localAudioTrack.enabled = true;
localStream.addTrack(localAudioTrack);
localVideoTrack = stream.getVideoTracks()[0];
localVideoTrack.enabled = true;
localStream.addTrack(localVideoTrack);
localVideo = document.createElement('video');
videoContainer.append(localVideo);
localVideo.setAttribute('id','localVideo');
localVideo.srcObject = localStream;
localVideo.muted = true;
localVideo.play();
});
And now I get the prompt.现在我得到了提示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.