环境:vue2.0+h5+uniapp 本地测试环境,使用的是uc浏览器,微信测试环境不支持;
我是想做一个微信公众号的自定义的拍照界面,通过video+getUserMedia()方法实现,代码如下;
<template>
<div>
<video id="camera" autoplay></video>
<button @click="startCamera">启动摄像头</button>
<button @click="stopCamera">停止摄像头</button>
</div>
</template>
startCamera() {
const constraints = {
audio: false,
video: {
// 前置摄像头
facingMode: "user",
// 该属性相当于手机端的高
width: Math.max(windowWidth, windowHeight) - 120, // 减去 120 用于在页面底部放置拍照等功能按钮
// 该属性相当于手机端的宽
height: Math.min(windowWidth, windowHeight),
· },
};
navigator.mediaDevices.getUserMedia(constraints)
.then(function(stream) {
const video = document.querySelector("video");
alert(video)
video.srcObject = stream;
video.onloadedmetadata = function(e) {
video.play();
};
})
.catch(function(err) {
console.log(err.name + ": " + err.message);
alert(err.name + ": " + err.message);
});
}
启动摄像头后弹出alert(video对象)之后就没有反应了,为什么