网页中使用navigator.mediaDevices.getUserMedia接口调用摄像头,在安卓端可以正常使用,safari浏览器也可以使用,但是使用小程序webview加载网页之后没法调用摄像头。我需要在摄像机画面前面展示3D模型, 而input type=file是调用手机自身相机,会覆盖掉小程序内容。
/**调用摄像头 */
testVideo() {
let div = Laya.Browser.window.document.createElement(“div”);
div.innerHTML = “<video id = ‘video’></video>”;
document.body.appendChild(div)
let video = Laya.Browser.window.document.getElementById(‘video’);
let constraints = {
video: { width: 800, height: 800 },
audio: false
};
if (navigator.mediaDevices === undefined) {
navigator.mediaDevices = {};
}
if (navigator.mediaDevices != undefined && navigator.mediaDevices.getUserMedia != undefined) { // 标准的API
navigator.mediaDevices.getUserMedia(constraints).then(stream => {
video.srcObject = stream;
video.onloadedmetadata = (e) => {
alert("play: " + stream);
video.play();
}
});
} else if (navigator.webkitGetUserMedia != undefined) { // WebKit 核心的API
navigator.webkitGetUserMedia(constraints, stream => {
alert("play: " + window.webkitURL.createObjectURL(stream));
video.src = window.webkitURL.createObjectURL(stream);
video.play();
}, err => {
alert("kitErr: " + err.message);
});
} else {
alert("defaultErr: " + err.message);
}
}