admin管理员组文章数量:1332865
The following code does pretty much what I want:
video = document.querySelector('video');
video.srcObject = new MediaStream();
navigator.mediaDevices.getUserMedia({video: true}).then(stream => {
stream.getTracks().forEach(track => {
video.srcObject.addTrack(track);
});
});
setTimeout(() => {
video.srcObject.getVideoTracks().forEach(track => {
track.stop()
video.srcObject.removeTrack(track);
});
}, 5000);
It starts the camera and displays the result on screen. After 5 seconds the camera is stopped again. However, the last image from the camera remains as a freeze frame in the video. How can I clear the video element?
(Note that I only remove the video tracks, so there might still be audio tracks playing.)
EDIT: I messed up the audio part of the question. In the real code there are audio tracks that should keep playing, I just forgot to add them to the example.
The following code does pretty much what I want:
video = document.querySelector('video');
video.srcObject = new MediaStream();
navigator.mediaDevices.getUserMedia({video: true}).then(stream => {
stream.getTracks().forEach(track => {
video.srcObject.addTrack(track);
});
});
setTimeout(() => {
video.srcObject.getVideoTracks().forEach(track => {
track.stop()
video.srcObject.removeTrack(track);
});
}, 5000);
It starts the camera and displays the result on screen. After 5 seconds the camera is stopped again. However, the last image from the camera remains as a freeze frame in the video. How can I clear the video element?
(Note that I only remove the video tracks, so there might still be audio tracks playing.)
EDIT: I messed up the audio part of the question. In the real code there are audio tracks that should keep playing, I just forgot to add them to the example.
Share Improve this question edited Mar 24, 2020 at 5:21 tobib asked Mar 23, 2020 at 23:07 tobibtobib 2,4544 gold badges25 silver badges38 bronze badges1 Answer
Reset to default 6Your code is removing all the tracks from the MediaStream, not only the video ones. To remove only the video ones, you'd do
video.srcObject.getVideoTracks().forEach(track => {
track.stop()
video.srcObject.removeTrack(track);
});
Note how getTracks
is replaced with getVideoTracks
.
But even then, the last frame from the video track may very well still stay in the <video>
element. To get rid of it, but keep the audio tracks, you'd be better creating a new MediaStream from the audio tracks and set this as the new srcObject of your <video>
:
Code of this fiddle since StackSnippet's iframe aren't allowed to call getUserMedia.
(async ()=>{
const video = document.querySelector('video');
const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true } );
video.srcObject = stream;
video.play();
btn.onclick = e => {
stream.getVideoTracks().forEach( vidTrack => vidTrack.stop() );
const new_stream = new MediaStream(stream.getAudioTracks());
video.srcObject = new_stream;
btn.remove();
}
})();
<button id="btn">keep only audio</button>
<video controls autoplay></video>
And if you wanted to replace it with a solid color (e.g a black frame), then you could pass a CanvasCaptureStreamTrack along with the audio tracks, and draw that solid color on the source <canvas>
: fiddle
本文标签: javascriptHow to clear html video element when removing trackStack Overflow
版权声明:本文标题:javascript - How to clear html video element when removing track - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742286277a2446985.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论