Last active
April 4, 2024 17:11
-
-
Save m3g4p0p/dd05494db42e87bb3d033d9f375ae7ab to your computer and use it in GitHub Desktop.
Capture a video stream and play it with some delay
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const DELAY = 2000 | |
const videos = [] | |
const hide = el => el.style.display = 'none' | |
const show = el => el.style.display = 'block' | |
const showAndHideOthers = ({ target }) => { | |
window.requestAnimationFrame(() => { | |
videos.forEach(hide) | |
show(target) | |
}) | |
} | |
const initRecorder = stream => { | |
const recorder = new MediaRecorder(stream) | |
const video = document.createElement('video') | |
const restart = ({ data }) => { | |
video.src = URL.createObjectURL(data) | |
recorder.start() | |
} | |
video.autoplay = true | |
videos.push(video) | |
video.addEventListener('play', showAndHideOthers) | |
document.body.appendChild(video) | |
recorder.addEventListener('dataavailable', restart) | |
recorder.start() | |
window.setInterval(recorder.stop.bind(recorder), DELAY) | |
} | |
navigator | |
.mediaDevices | |
.getUserMedia({ video: true }) | |
.then(stream => { | |
initRecorder(stream) | |
window.setTimeout(initRecorder, DELAY / 2, stream) | |
}) | |
.catch(console.error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment