gpt4 book ai didi

javascript - DOMException : play() can only be initiated by a user gesture

转载 作者:IT老高 更新时间:2023-10-28 23:22:40 29 4
gpt4 key购买 nike

我正在开发一个带有 JavaScriptQRCode Reader。如果用户在我的网站上,它会请求使用设备摄像头的权限。一旦用户接受它,它就会打开前摄像头。我正在使用带有最新 Chrome 版本的三星 Galaxy S4,目前运行良好。

我添加了一个下拉菜单,可以将前摄像头更改为后摄像头。一旦我更换相机,视频流就会停止,并且出现此错误。

Uncaught (in promise) DOMException: play() can only be initiated by a user gesture.

我已经在旧版 Chrome 上进行了尝试,即使 camare 发生变化也能正常工作。

            var videoElement = document.createElement("video");
var videoSelect = document.querySelector("select#videoSource");

navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;

function start() {
if (window.stream) {
videoElement.src = null;
window.stream.stop();
}
var videoSource = videoSelect.value;
var tw = 640 // 320 // 640 // 1280;
var th = 480 // 240 // 480 // 720

var hdConstraints = {
audio: false,
video: {
mandatory: {
maxWidth: tw,
maxHeight: th
},
optional: [{
sourceId: videoSource
}]
}
};
if (navigator.getUserMedia) {
navigator.getUserMedia(hdConstraints, success, errorCallback);
} else {
errorCallback("");
}
}

videoSelect.onchange = start;
start();

function gotSources(sourceInfos) {
for (var i = 0; i !== sourceInfos.length; ++i) {
var sourceInfo = sourceInfos[i];
var option = document.createElement("option");
option.value = sourceInfo.id;

if (sourceInfo.kind === "video") {
option.text = sourceInfo.label || "camera " + (videoSelect.length + 1);
videoSelect.appendChild(option);
} else {
console.log("Some other kind of source: ", sourceInfo);
}

}
}

if (typeof MediaStreamTrack === "undefined") {
alert("This browser does not support MediaStreamTrack.\n\nTry Chrome.");
} else {
MediaStreamTrack.getSources(gotSources);
}

function errorCallback(e) {
console.log("Cant access user media", e);
}

function success(stream) {

window.stream = stream;
videoElement.src = window.URL.createObjectURL(stream);
videoElement.onclick = function() { videoElement.play(); };
videoElement.play(); //Here is the Error


function getFrame() {
requestAnimationFrame(getFrame);

if (!videoElement.videoWidth) return;

if (!image) {
width = videoElement.videoWidth, height = videoElement.videoHeight;
log("videoElement", width, height, videoElement);

var canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
canvas.style.transform = "scale(1, 1)";

ctx = canvas.getContext("2d");
document.body.appendChild(canvas);

log("start");
image = Module._xsetup(width, height);
log("_xsetup", image, "pointer");
return;
}

ctx.drawImage(videoElement, 0, 0, width, height);
var imageData = ctx.getImageData(0,0, width, height);
data = imageData.data;
gofill();
}

getFrame();

}

最佳答案

这可能会有所帮助。

webView.getSettings().setMediaPlaybackRequiresUserGesture(false);

关于javascript - DOMException : play() can only be initiated by a user gesture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38791760/

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