[英]navigator.mediaDevices.getUserMedia permission denied
I am trying to navigator.mediaDevices.getUserMedia to select a camera from a dropdown and use it, here is my code that gets my devices:我正在尝试从下拉列表中将 navigator.mediaDevices.getUserMedia 到 select 相机并使用它,这是获取我的设备的代码:
var video = document.getElementById('video');
navigator.mediaDevices.enumerateDevices().then(function (devices) {
for (var i = 0; i < devices.length; i++) {
var device = devices[i];
if (device.kind === 'videoinput') {
var option = document.createElement('option');
option.value = device.deviceId;
option.text = device.label || 'camera ' + (i + 1);
document.querySelector('select#videoSource').appendChild(option);
}
};
});
and here is my on change code to use the camera:这是我使用相机的更改代码:
$("#videoSource").on("change", function () {
var x = $(this).value;
var constraints = { deviceId: { exact: x } };
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: constraints }).then(function (stream) {
video.srcObject = stream;
video.play();
});
}
});
When I goto select an option to use a camera I get this error:当我转到 select 使用相机的选项时,我收到此错误:
Uncaught (in promise) DOMException: Permission denied
未捕获(承诺)DOMException:权限被拒绝
What am I doing wrong?我究竟做错了什么?
I have test your code in Stackblitz and it works for me!我已经在 Stackblitz 中测试了你的代码,它对我有用!
For triggering the change event in the select, I have added an extra option for selection.为了触发 select 中的更改事件,我添加了一个额外的选项供选择。
Maybe your problem is that you have denied the permissions.也许您的问题是您拒绝了权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.