From d87ee1a55a7bd8358409cb43aa28376e85101eef Mon Sep 17 00:00:00 2001 From: ashley Date: Sun, 5 Oct 2025 14:32:13 +0200 Subject: [PATCH] test something --- css/player-base-new.js | 80 +++++++++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 25 deletions(-) diff --git a/css/player-base-new.js b/css/player-base-new.js index 39eb425d..d9eca9b1 100644 --- a/css/player-base-new.js +++ b/css/player-base-new.js @@ -2,7 +2,9 @@ var _yt_player = videojs; var versionclient = "youtube.player.web_20250917_22_RC00" - // video.js 8 init - source can be seen in https://poketube.fun/static/vjs.min.js or the vjs.min.js file + + +// video.js 8 init - source can be seen in https://poketube.fun/static/vjs.min.js or the vjs.min.js file document.addEventListener("DOMContentLoaded", () => { const player = videojs('video', { controls: true, @@ -59,8 +61,12 @@ document.addEventListener("DOMContentLoaded", () => { // guards to prevent feedback loops / re-entrancy let volGuard = false; - let syncGuard = false; // prevents re-entrant sync work - let couplingGuard = false; // prevents play/pause echo loops + let syncGuard = false; // prevents re-entrant sync work + let couplingGuard = false; // prevents play/pause echo loops + + // lightweight autosyncer (only when playing) + let autosyncTimer = null; + const AUTOSYNC_MS = 400; // last time to detect wrap (loop) let lastVideoTime = 0; @@ -98,7 +104,6 @@ document.addEventListener("DOMContentLoaded", () => { try { return !!player.loop?.() || !!videoEl.loop; } catch { return !!videoEl.loop; } } - // === VOLUME / MUTE MIRROR (both ways) === function mirrorFromPlayerVolumeMute() { if (volGuard) return; volGuard = true; try { @@ -125,13 +130,13 @@ document.addEventListener("DOMContentLoaded", () => { audioEl.addEventListener('volumechange', mirrorFromAudioVolumeMute); player.ready(() => mirrorFromPlayerVolumeMute()); - // === MASTER/SLAVE COUPLING (Video.js is master; STRICT) === function playBoth() { if (couplingGuard) return; couplingGuard = true; // start video first so it owns the session; audio follows immediately player.play()?.catch(()=>{}); audioEl.play()?.catch(()=>{}); + startAutosync(); couplingGuard = false; } function pauseBoth() { @@ -139,19 +144,19 @@ document.addEventListener("DOMContentLoaded", () => { couplingGuard = true; player.pause(); audioEl.pause(); + stopAutosync(); couplingGuard = false; } // absolutely enforce: whenever video plays/pauses, audio mirrors (and vice-versa is blocked) player.on('play', () => { - // ensure audio follows a user/UI-initiated play if (audioEl.paused) audioEl.play()?.catch(()=>{}); - // mark mediaSession state + startAutosync(); try { if ('mediaSession' in navigator) navigator.mediaSession.playbackState = 'playing'; } catch {} }); player.on('pause', () => { - // ensure audio pauses when video pauses if (!audioEl.paused) audioEl.pause(); + stopAutosync(); try { if ('mediaSession' in navigator) navigator.mediaSession.playbackState = 'paused'; } catch {} }); @@ -163,7 +168,6 @@ document.addEventListener("DOMContentLoaded", () => { if (!player.paused()) pauseBoth(); }); - // === MEDIA SESSION (stable timeline + working seek) === let lastMSPos = 0; let lastMSAt = 0; const MS_THROTTLE_MS = 250; @@ -261,7 +265,6 @@ document.addEventListener("DOMContentLoaded", () => { if (!player.paused()) playBoth(); }); - // keep MS timeline sane but light player.on('timeupdate', () => updateMSPositionState(true)); player.on('ratechange', () => updateMSPositionState(false)); player.on('loadedmetadata', () => { lastMSPos = 0; updateMSPositionState(false); }); @@ -274,7 +277,6 @@ document.addEventListener("DOMContentLoaded", () => { mediaSessionReady = true; } - // ** DESKTOP MEDIA-KEY FALLBACK ** document.addEventListener('keydown', e => { switch (e.code) { case 'AudioPlay': @@ -300,18 +302,15 @@ document.addEventListener("DOMContentLoaded", () => { } }); - // === EVENT-DRIVEN SYNC (no timers / no rAF) ============================== - // Video is master; we only adjust audio on video 'timeupdate'. - player.on('timeupdate', () => { + function doSyncOnce() { if (syncGuard) return; const vt = Number(player.currentTime()); const at = Number(audioEl.currentTime); if (!isFinite(vt) || !isFinite(at)) return; - // detect wrap (loop) via time going backwards on the