当用户在调用 getUserMedia 时拒绝使用麦克风时,Firefox 不会引发错误吗?此代码在 Firefox 中不会记录错误,但在 Chrome 中会记录错误。
这是我正在使用的代码:
navigator.mediaDevices.
getUserMedia(media).
then(function(stream) {
// doing some stuff here
}).
catch(function(error) {
// log errors
console.log(error);
});
我缺少什么吗?
编辑:我刚刚发现,如果我在权限对话框中单击“现在不”,则不会触发任何错误。如果我单击“从不用于此网站”,则会引发错误,但这种行为远非理想,因为我没有机会通知用户,以防他(意外)单击“现在不”而不是授予权限。
最佳答案
是的,它在 Firefox 上运行。
但这是设计使然,当用户单击“现在不”时,请求仍然处于事件状态;用户仍然可以通过单击右侧的 URL Pane 图标来允许访问媒体设备。因此您不会得到任何反馈。 是的,在 Chrome 上您会收到错误消息,但在 Firefox 上不会出现错误。
You need to use timers to detect such scenario(hackish).
有关更多信息,请参阅此线程:https://bugzilla.mozilla.org/show_bug.cgi?id=947266
If the user provides permission, then the returned Promise is resolved with the resulting MediaStream object. If the user denies permission, or media is not available, then the promise is rejected with PermissionDeniedError or NotFoundError respectively.
navigator.mediaDevices.getUserMedia({audio: true, video: true }).then(function(stream) {
/* use the stream */
}).catch(function(err) {
alert(err)
});
关于javascript - 用户拒绝在 Firefox 中使用麦克风的权限时,getUserMedia 静默失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43316453/