我偶然发现了一种效果很好的方法,并且不需要 cookie(人们可以带外更改这些设置,因此 cookie 可能会变得不正确),或者 Chrome 提供一点帮助(它不需要't)。
if (chrome) {
var madeMicDecision = false;
navigator.getUserMedia({whatever},
function(stream) {
madeMicDecision = true;
//close the stream, move to the next page
//they have either just granted permission, or have in the past and it's remembered
},
function(error) {
madeMicDecision = true;
//go to error handling page
//the error handling page should describe going to chrome://settings/contentExceptions#media-stream
//or how to allow the user media via clicking on the crossed out camera icon in the 'wonderbar'
}
}
但是我们还想显示教程屏幕——如果没有记住权限设置,上面的代码将弹出 chrome 信息框。但是如果没有来自 chrome 的权限事件,或者没有用于查询权限的 API,我们如何知道何时显示 chrome+infobox 教程内容?
我的诀窍是自动显示内容,但只能在延迟之后。以前接受或拒绝权限的人永远不会看到此内容,因为我们的成功/错误处理程序会在延迟之前将它们移开。所以,给 getUserMedia 调用一点时间来检测/反应 - 如果浏览器没有继续前进,是时候显示帮助内容了。
setTimeout(function() {
if (!madeMicDescision) {
//display chrome/infobox/getUserMedia permissions help content
}
}, 2000);
不完美(真的很奇怪和悲伤),但似乎适用于最常见的情况。