Update html/poketube.ejs

This commit is contained in:
ashley 2024-08-19 22:18:23 +00:00
parent ad28847a67
commit 4f901e602a

View file

@ -619,101 +619,91 @@ background-color: #0000;
</style> </style>
<script> <script>
document.addEventListener("DOMContentLoaded", () => { document.addEventListener("DOMContentLoaded", () => {
const video = videojs('video', { const video = videojs('video', {
controls: true, controls: true,
autoplay: false, autoplay: false,
preload: 'auto', preload: 'auto',
});
const qua = new URLSearchParams(window.location.search).get("quality") || ""; });
localStorage.setItem(`progress-${new URLSearchParams(window.location.search).get('v')}`, 0);
if (qua !== "medium") { const qua = new URLSearchParams(window.location.search).get("quality") || "";
const audio = document.getElementById('aud'); localStorage.setItem(`progress-${new URLSearchParams(window.location.search).get('v')}`, 0);
// Sync volume between audio and video if (qua !== "medium") {
const syncVolume = () => { const audio = document.getElementById('aud');
audio.volume = video.volume();
};
const syncVolumeWithVideo = () => { // Sync volume between audio and video
video.volume(audio.volume); const syncVolume = () => {
}; audio.volume = video.volume();
};
const checkAudioBuffer = () => { const syncVolumeWithVideo = () => {
const buffered = audio.buffered; video.volume(audio.volume);
const bufferedEnd = buffered.length > 0 ? buffered.end(buffered.length - 1) : 0; };
return audio.currentTime <= bufferedEnd;
};
const isVideoBuffered = () => { const checkAudioBuffer = () => {
// Check if video has enough buffered data const buffered = audio.buffered;
const buffered = video.buffered(); const bufferedEnd = buffered.length > 0 ? buffered.end(buffered.length - 1) : 0;
return buffered.length > 0 && buffered.end(buffered.length - 1) >= video.currentTime(); return audio.currentTime <= bufferedEnd;
}; };
const handleSeek = () => { const isVideoBuffered = () => {
// Pause video and audio when seeking // Check if video has enough buffered data
video.pause(); const buffered = video.buffered();
audio.pause(); return buffered.length > 0 && buffered.end(buffered.length - 1) >= video.currentTime();
};
// Sync audio with video during seeking const handleSeek = () => {
if (Math.abs(video.currentTime() - audio.currentTime) > 0.3) { // Pause video and audio when seeking
audio.currentTime = video.currentTime(); video.pause();
} audio.pause();
// Wait for audio to be buffered sufficiently // Sync audio with video during seeking
if (!checkAudioBuffer()) { if (Math.abs(video.currentTime() - audio.currentTime) > 0.3) {
audio.addEventListener('canplay', () => { audio.currentTime = video.currentTime();
if (video.paused && isVideoBuffered()) {
video.play();
audio.play();
} }
}, { once: true });
}
};
video.on('play', () => { // Wait for audio to be buffered sufficiently
// Set initial sync point at 0.01 seconds if (!checkAudioBuffer()) {
video.currentTime(0.01); audio.addEventListener('canplay', () => {
audio.currentTime = 0.01; if (video.paused && isVideoBuffered()) {
video.play();
// Wait for both video and audio to be buffered sufficiently audio.play();
if (isVideoBuffered() && checkAudioBuffer()) { }
video.play(); }, { once: true });
audio.play();
} else {
video.pause();
audio.pause();
const bufferListener = () => {
if (isVideoBuffered() && checkAudioBuffer()) {
video.play();
audio.play();
audio.removeEventListener('canplay', bufferListener);
} }
}; };
audio.addEventListener('canplay', bufferListener); video.on('play', () => {
} if (Math.abs(video.currentTime() - audio.currentTime) > 0.3) {
}); audio.currentTime = video.currentTime();
}
video.on('pause', () => { if (isVideoBuffered()) {
audio.pause(); audio.play();
}); } else {
video.pause();
}
});
video.on('seeking', handleSeek); video.on('pause', () => {
audio.pause();
});
video.on('seeked', () => {
if (isVideoBuffered()) {
video.play();
}
audio.play(); // Ensure audio is playing after seek
});
video.on('volumechange', syncVolume);
audio.addEventListener('volumechange', syncVolumeWithVideo); video.on('seeking', handleSeek);
video.on('seeked', () => {
if (isVideoBuffered()) {
video.play();
}
audio.play(); // Ensure audio is playing after seek
});
video.on('volumechange', syncVolume);
audio.addEventListener('volumechange', syncVolumeWithVideo);
// Listen for media control events // Listen for media control events
document.addEventListener('play', () => { document.addEventListener('play', () => {
@ -725,15 +715,14 @@ document.addEventListener("DOMContentLoaded", () => {
video.pause(); video.pause();
audio.pause(); audio.pause();
}); });
document.addEventListener('fullscreenchange', () => {
document.addEventListener('fullscreenchange', () => { if (!document.fullscreenElement) {
if (!document.fullscreenElement) { video.pause();
video.pause(); audio.pause();
audio.pause(); }
});
} }
}); });
}
});
</script> </script>