【发布时间】:2021-09-01 04:03:23
【问题描述】:
我正在开发一个应用程序,用户可以在其中使用前置/稀有摄像头拍摄照片。它工作得很好,但是当切换摄像头前端/罕见的 var playPromise = videoStream.play() 时,它处于挂起状态。有时承诺得到解决,相机工作有时不。
这个问题只出现在 chrome 浏览器中,而不是 mozila 和 firefox 中
try { stopWebCam(); // stop media stream when toggle over camera stream = await navigator.mediaDevices.getUserMedia({video: true}); /* use the stream */ let videoStream = document.getElementById('captureCandidateId'); videoStream.srcObject = stream; // videoStream.play(); var playPromise = videoStream.play(); if (playPromise !== undefined) { playPromise.then(_ => { // Automatic playback started! // Show playing UI. }) .catch(error => { // Auto-play was prevented // Show paused UI. }); } }; } catch(err) { /* handle the error */ console.log(err.name + ": " + err.message); }
let stopWebCam = function (pictureType) { setTimeout(()=>{ let videoStream = document.getElementById('captureCandidateId'); const stream = videoStream.srcObject; if (stream && stream.getTracks) { const tracks = stream.getTracks(); tracks.forEach(function(track) { track.stop(); }); } videoStream.srcObject = null; }, 0) }
【问题讨论】:
标签: javascript html google-chrome video.js mediaelement.js