<html
>
<body
>
<video id
="webcam"></video
>
<button onClick
="openVideo()">开启摄像头
</button
>
<button onClick
="closeVideo()">关闭摄像头
</button
>
</body
>
<script
>
var video
= document
.getElementById('webcam');
function onSuccess(stream
) {
if (window
.URL) {
video
.srcObject
= window
.srcObject
= stream
} else {
video
.srcObject
= stream
;
}
video
.autoplay
= true;
}
function onError(stream
) {
console
.log('no')
}
if (navigator
.getUserMedia
) {
navigator
.getUserMedia({video
:true}, onSuccess
,onError
);
} else {
document
.getElementById('webcam').src
= '事先准备好的错误视频.mp4';
}
function closeVideo(){
video
.srcObject
.getTracks()[0].stop();
video
.srcObject
.getTracks()[0].stop();
}
function openVideo(){
navigator
.getUserMedia({video
:true}, onSuccess
,onError
);
}
</script
>
</html
>
转载请注明原文地址:https://tech.qufami.com/read-11336.html