Skip to content

Instantly share code, notes, and snippets.

@troy-lamerton
Last active March 21, 2021 05:09
Show Gist options
  • Save troy-lamerton/252f441bbc273f4676be789bdb1b8a76 to your computer and use it in GitHub Desktop.
Save troy-lamerton/252f441bbc273f4676be789bdb1b8a76 to your computer and use it in GitHub Desktop.
function preloadVideo(video) {
// just before video would play, pause it
video.addEventListener('canplay', preloadingVideoCanPlay, {once: true});
// begin preloading the playlist and first hls segment (useful for VODs)
video.play();
}
function preloadingVideoCanPlay() {
// video has buffered enough for playback
video.pause();
}
function playVideo(video) {
video.removeEventListener('canplay', preloadingVideoCanPlay);
video.play();
}
// 1. preloadVideo(video)
// ... some time later ...
// 2. playVideo(video)
function preloadVideo(video) {
video.muted = true;
// just before video would play, pause it
video.addEventListener('canplay', preloadingVideoCanPlay, {once: true});
// begin preloading the playlist and first hls segment (useful for VODs)
video.play();
}
function preloadingVideoCanPlay() {
// video has buffered enough for playback
video.pause();
}
function playVideo(video) {
video.removeEventListener('canplay', preloadingVideoCanPlay);
video.play();
}
// 1. preloadVideo(video)
// ... some time later ...
// 2. *tap to enter community* -> playVideo(video)
// 3. on entered community (web view will appear now) -> video.muted = false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment