gpt4 book ai didi

javascript - getUserMedia 不适用于新浏览器

转载 作者:行者123 更新时间:2023-11-28 05:24:54 47 4
gpt4 key购买 nike

我正在使用 HTML 媒体捕获和 getUserMedia 方法。它不适用于 Chrome,我收到失败时的警报。

这是我使用的示例代码:

if (navigator.getUserMedia) {
navigator.getUserMedia(
// constraints
{
video: true,
audio: true
},
// successCallback
function (localMediaStream) {
var video = document.querySelector('video');
video.src = window.URL.createObjectURL(localMediaStream);
// Do something with the video
video.play();
},
// errorCallback
function (err) {
console.log("The following error occured: " + err);
}
);
} else {
alert("getUserMedia not supported by your web browser or Operating system version");
}

最佳答案

Chrome 无法识别标准的 navigator.getUserMedia。它适用于 Microsoft Edge。您将需要添加 vendor 前缀。对于 Chrome:navigator.webkitGetUserMedia

这是 JSFiddle 上的工作代码 https://jsfiddle.net/RamiSarieddine/t9d3hpyr/

//browser support check "ms" vendor function is for IE8
navigator.getUserMedia = ( navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia );

if (navigator.getUserMedia) {
navigator.getUserMedia(
// constraints
{
video: true,
audio: true
},
// successCallback
function (localMediaStream) {
var video = document.querySelector('video');
video.src = window.URL.createObjectURL(localMediaStream);
// Do something with the video
video.play();
},
// errorCallback
function (err) {
console.log("The following error occured: " + err);
}
);
} else {
alert("getUserMedia not supported by your web browser or Operating system version");
}

关于javascript - getUserMedia 不适用于新浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33985806/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com