简体   繁体   English

即使在安全环境中,Chrome 也不会提示用户访问摄像头和/或麦克风

[英]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.isSecureContexttrue
  • 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.

相关问题 chrome扩展程序中的Twilio-用户拒绝访问麦克风 - Twilio in chrome extension - user denied access to microphone 为什么Chrome不会显示带麦克风和网络摄像头的“允许”或“禁止”权限的提示? - Why does Chrome not show the prompt with Allow or Disallow permission to microphone and webcam? Chrome 不允许 HTTP 托管站点访问相机和麦克风 - Chrome is not letting HTTP hosted site to access Camera & Microphone 如何在Chrome OS或Chrome扩展程序的打包应用程序中启用相机和麦克风? - How to enable camera and microphone in packaged application for Chrome OS or Chrome extension? iOS 12上的麦克风访问FIrefox和Chrome - Microphone access FIrefox and Chrome on iOS 12 Chrome:不“记住”允许访问麦克风的选择 - Chrome: not “remembering” the choice to allow access to microphone 有没有一种方法可以请求访问Chrome扩展程序弹出窗口的麦克风? - Is there a way to request microphone access for a chrome extension popup? Facebook 浏览器不请求访问麦克风 - Facebook browser does not request access to the microphone 如何通过getUserMedia在Google Chrome中访问麦克风? - How do I get microphone access in Google Chrome through getUserMedia? 需要麦克风访问权限的用户脚本会多次提示您在Chrome中的权限 - Userscript that requires microphone access prompts for permissions in Chrome multiple times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM