stuff stuff

This commit is contained in:
ashley 2024-09-28 13:19:14 +00:00
parent b4d566f41f
commit f5f88fe072

View file

@ -26,34 +26,35 @@ document.addEventListener("DOMContentLoaded", () => {
}; };
const isVideoBuffered = () => { const isVideoBuffered = () => {
// Check if video has enough buffered data
const buffered = video.buffered(); const buffered = video.buffered();
return buffered.length > 0 && buffered.end(buffered.length - 1) >= video.currentTime(); return buffered.length > 0 && buffered.end(buffered.length - 1) >= video.currentTime();
}; };
const handleSeek = () => { const handleSeek = () => {
// Pause video and audio when seeking
video.pause(); video.pause();
audio.pause(); audio.pause();
// Sync audio with video during seeking
if (Math.abs(video.currentTime() - audio.currentTime) > 0.3) { if (Math.abs(video.currentTime() - audio.currentTime) > 0.3) {
audio.currentTime = video.currentTime(); audio.currentTime = video.currentTime();
} }
// Wait for audio to be buffered sufficiently
if (!checkAudioBuffer()) { if (!checkAudioBuffer()) {
audio.addEventListener('canplay', () => { audio.addEventListener('canplay', () => {
if (video.paused && isVideoBuffered()) { if (video.paused && isVideoBuffered()) {
video.play(); video.play();
audio.play(); audio.play();
} }
}, { }, { once: true });
once: true
});
} }
}; };
const handleBufferingComplete = () => {
if (Math.abs(video.currentTime() - audio.currentTime) > 0.3) {
audio.currentTime = video.currentTime();
}
};
// Sync when playback starts
video.on('play', () => { video.on('play', () => {
if (Math.abs(video.currentTime() - audio.currentTime) > 0.3) { if (Math.abs(video.currentTime() - audio.currentTime) > 0.3) {
audio.currentTime = video.currentTime(); audio.currentTime = video.currentTime();
@ -74,12 +75,16 @@ document.addEventListener("DOMContentLoaded", () => {
if (isVideoBuffered()) { if (isVideoBuffered()) {
video.play(); video.play();
} }
audio.play(); // audio is playing after seek audio.play();
}); });
video.on('volumechange', syncVolume); video.on('volumechange', syncVolume);
audio.addEventListener('volumechange', syncVolumeWithVideo); audio.addEventListener('volumechange', syncVolumeWithVideo);
// Detect when video or audio finishes buffering
video.on('canplaythrough', handleBufferingComplete);
audio.addEventListener('canplaythrough', handleBufferingComplete);
// Listen for media control events // Listen for media control events
document.addEventListener('play', (e) => { document.addEventListener('play', (e) => {
if (e.target === video) { if (e.target === video) {