当前位置: 首页 > 图灵资讯 > 技术篇> 218-H5页面,打开手机摄像头和关闭手机摄像头

218-H5页面,打开手机摄像头和关闭手机摄像头

来源:图灵教育
时间:2023-11-19 17:27:50

let cameraStream = null;navigator.mediaDevices.getUserMedia({ video: { facingMode: 'user' } })  .then(function (stream) {    const cameraPreview = document.getElementById('cameraPreview');    cameraPreview.srcObject = stream;    cameraStream = stream;  })  .catch(function (error) {    console.error('Error accessing the camera:', error);  });const stopCameraButton = document.getElementById('stopCameraButton');stopCameraButton.addEventListener('click', function () {  if (cameraStream) {    const tracks = cameraStream.getTracks();    tracks.forEach(track => track.stop());    cameraStream = null;    const cameraPreview = document.getElementById('cameraPreview');    cameraPreview.srcObject = null;  }});