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