我正在尝试使用 navigator.mediaDevices.getUserMedia 以纵向方式启动后置摄像头,因为相机似乎没有考虑我发送的约束这是代码如何查找初始化
const constraints = {
video: {
aspectRatio: 6/19,
width: 1920,
height: 1080,
facingMode: "environment",
},
audio: false,
};
navigator.mediaDevices.getUserMedia(constraints)
.then((mediaStream) => {
video = document.querySelector('video');
video.srcObject = mediaStream;
video.onloadedmetadata = () => {
video.play();
};
})
.catch((err) => {
console.error(`${err.name}: ${err.message}`);
});
<video autoPlay={true} playsInline={true} muted={true} id="video" width="100%" height="100%"></video>
我给你一个HTML页面供你参考:
async function initCam() {
try {
let constraints = {
"video": {
facingMode: "environment"
}
};
let stream = await navigator.mediaDevices.getUserMedia(constraints);
let video = document.getElementById("qq");
video.srcObject = stream;
} catch (err) {
console.log(err);
};
}
.container {
display: flex;
flex-grow: 1 1;
}
video {
object-fit: cover;
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 100%;
}
<body onload="initCam()">
<div class="container">
<video autoplay muted controls id="qq" />
</div>
</body>
PS:请在手机上运行,我的PC chrome没有“facingMode”约束。