简体   繁体   English

getUserMedia() 允许用户继续,即使权限被拒绝

[英]getUserMedia() allow user to continue even if permission is denied

My sample getUserMedia() asks for permission for the camera and microphone.我的示例 getUserMedia() 请求相机和麦克风的许可。

let constraints = { audio: true, video: true };

navigator.mediaDevices.getUserMedia(constraints).then(stream => {
    // Do stuff
}).catch(e => alert(`getUserMedia error ${e.name}`))

If the user doesn't accept the camera and/or microphone permission request I get an alert error as expected.如果用户不接受相机和/或麦克风权限请求,我会按预期收到警报错误。

Is it possible to still ask for audio and video, but if the user refuses, then their devices won't be used but, would still allow the user to browse the site?是否仍然可以要求音频和视频,但如果用户拒绝,那么他们的设备将不会被使用,但仍然允许用户浏览网站?

Thank you.谢谢你。

If you want your code to run in either cases, then you'll have to split up and re-arrange your code in a way so that you can run it.如果您希望您的代码在任何一种情况下都能运行,那么您必须以某种方式拆分和重新排列您的代码,以便您可以运行它。

If the user accepts the stream, then go with doStuffWithStream , if not then call doStuffWithoutStream .如果用户接受 stream,则 go 与doStuffWithStream ,如果没有,则调用doStuffWithoutStream

If something needs to happen in both cases, then you could use the finally method to run code in both cases.如果在这两种情况下都需要发生某些事情,那么您可以使用finally方法在这两种情况下运行代码。

function doStuffWithStream(stream) {
  // Do stuff with stream.
}

function doStuffWithoutStream() {
  // Do stuff without stream.
}

function doStuffEitherWay() {
  // Do stuff in both cases.
}

let constraints = { audio: true, video: true };

navigator.mediaDevices.getUserMedia(constraints).then(stream => {
  doStuffWithStream(stream);
}).catch(e => {
  alert(`getUserMedia error ${e.name}`);
  doStuffWithoutStream();
}).finally(() => {
  doStuffEitherWay();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击“允许”按钮后,getUserMedia权限被拒绝 - getUserMedia permission denied after clicking the “allow” button OT.Publisher 访问被拒绝:最终用户拒绝访问硬件设备 getUserMedia - OT.Publisher Access Denied: End-user denied permission to hardware devices getUserMedia Chrome 28应用,getUserMedia PERMISSION_DENIED - Chrome 28 App, getUserMedia PERMISSION_DENIED 为什么getUserMedia()在chrome中显示权限被拒绝错误 - Why getUserMedia() showing permission denied error in chrome DOMException: 权限被拒绝 - navigator.mediaDevices.getUserMedia(); - DOMException: Permission denied - navigator.mediaDevices.getUserMedia( ); navigator.mediaDevices.getUserMedia 权限被拒绝 - navigator.mediaDevices.getUserMedia permission denied Navigator.getUserMedia()在Android手机上运行时返回权限被拒绝 - Navigator.getUserMedia() returns permission denied when run on android phone Google chrome DOMException:系统拒绝 navigator.mediaDevices.getUserMedia 的权限 - Google chrome DOMException: Permission denied by system for navigator.mediaDevices.getUserMedia Firebase用户PERMISSION_DENIED:权限被拒绝 - Firebase user PERMISSION_DENIED: Permission denied 在 UWP javascript 应用程序中获取 iframe 中 getUserMedia 的用户权限 - Get user permission for getUserMedia in iframe within a UWP javascript app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM