webRTC 使用 getUserMedia()关闭和开启摄像头

tech2023-07-03  106

<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; // 或者 video.play(); } 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>
最新回复(0)