From 99067848457078f50d2527d32cd6218b61a2fcba Mon Sep 17 00:00:00 2001 From: CodeDevMLH <145071728+CodeDevMLH@users.noreply.github.com> Date: Thu, 12 Feb 2026 21:35:29 +0100 Subject: [PATCH] Enhance visibility change handling to manage video playback more effectively when tab visibility changes --- .../Web/mediaBarEnhanced.js | 54 +++++++++++++++++-- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/Jellyfin.Plugin.MediaBarEnhanced/Web/mediaBarEnhanced.js b/Jellyfin.Plugin.MediaBarEnhanced/Web/mediaBarEnhanced.js index df4cbed..5cb6bc0 100644 --- a/Jellyfin.Plugin.MediaBarEnhanced/Web/mediaBarEnhanced.js +++ b/Jellyfin.Plugin.MediaBarEnhanced/Web/mediaBarEnhanced.js @@ -3517,17 +3517,61 @@ const MediaBarEnhancedSettingsManager = { */ const initPageVisibilityHandler = () => { document.addEventListener("visibilitychange", () => { + const isVideoPlayerOpen = document.querySelector('.videoPlayerContainer:not(.hide)') || + document.querySelector('.youtubePlayerContainer:not(.hide)'); + if (document.hidden) { - console.log("Tab inactive - stopping all slideshow playback"); + // Stop slide timer if (STATE.slideshow.slideInterval) { STATE.slideshow.slideInterval.stop(); } - SlideshowManager.stopAllPlayback(); + + if (isVideoPlayerOpen) { + // Jellyfin video is playing --> full stop to free all resources + console.log("Tab inactive and Jellyfin player active - stopping all playback"); + SlideshowManager.stopAllPlayback(); + } else { + // Simple tab switch: stop all others, pause only the current + console.log("Tab inactive. Pausing current video, stopping others"); + const currentItemId = STATE.slideshow.itemIds?.[STATE.slideshow.currentSlideIndex]; + + // Stop all players except the current one + if (STATE.slideshow.videoPlayers) { + Object.keys(STATE.slideshow.videoPlayers).forEach(id => { + if (id === currentItemId) return; + const p = STATE.slideshow.videoPlayers[id]; + if (p) { + try { + if (typeof p.stopVideo === 'function') { + p.stopVideo(); + if (typeof p.clearVideo === 'function') p.clearVideo(); + } else if (p.tagName === 'VIDEO') { + p.pause(); + p.muted = true; + p.currentTime = 0; + } + } catch (e) { console.warn("Error stopping background player", id, e); } + } + }); + } + + // Pause only the current video + if (currentItemId) { + const player = STATE.slideshow.videoPlayers?.[currentItemId]; + if (player) { + try { + if (typeof player.pauseVideo === 'function') { + player.pauseVideo(); + } else if (player.tagName === 'VIDEO') { + player.pause(); + } + } catch (e) { console.warn("Error pausing video on tab hide:", e); } + } + } + } } else { - console.log("Tab active - resuming slideshow"); - // Only resume if we're on the home page and not paused + console.log("Tab active. Resuming slideshow"); const isOnHome = window.location.hash === "#/home.html" || window.location.hash === "#/home"; - const isVideoPlayerOpen = document.querySelector('.videoPlayerContainer:not(.hide)') || document.querySelector('.youtubePlayerContainer:not(.hide)'); if (isOnHome && !STATE.slideshow.isPaused && !isVideoPlayerOpen) { SlideshowManager.resumeActivePlayback();