From ba28518d8a106499f8f9cae7740164568573bb9b Mon Sep 17 00:00:00 2001 From: Honey Date: Thu, 28 May 2026 00:41:01 +0200 Subject: [PATCH] fix: NP overlay shows stale song on Android auto-advance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit openNowPlaying() was blindly trusting the MediaController snapshot index, which can lag one track behind ExoPlayer on auto-advance. This overwrote the already-correct S.qIdx that onTrackChanged had just set, causing the NP overlay to render the previous song while the mini-player showed the right one. Now only accept the snapshot index when the song ID at that position matches — stale snapshots are silently ignored. Co-Authored-By: Claude Sonnet 4.6 --- src/nowplaying.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/nowplaying.js b/src/nowplaying.js index ac27f6a..d591e67 100644 --- a/src/nowplaying.js +++ b/src/nowplaying.js @@ -8,13 +8,19 @@ import { openPlModal, showSongInfo } from './ui.js'; // ─── NOW PLAYING OVERLAY ─── export function openNowPlaying() { - // On Android, onTrackChanged is async — query native for the current index - // before rendering so we never show the previous song for a flash. + // On Android, verify the snapshot index before using it — the MediaController + // snapshot can lag behind ExoPlayer by one track on auto-advance. Only accept + // it when the song ID at that position matches (i.e. snapshot is not stale). if (typeof window.NSBridge !== 'undefined') { try { const state = JSON.parse(window.NSBridge.getState() || '{}'); - if (typeof state.currentIndex === 'number' && state.currentIndex >= 0 && state.currentIndex < S.queue.length) - S.qIdx = state.currentIndex; + const snapIdx = state.currentIndex; + const snapId = state.currentId || ''; + if (typeof snapIdx === 'number' && snapIdx >= 0 && snapIdx < S.queue.length) { + const expected = S.queue[snapIdx]; + if (!snapId || !expected?.id || String(snapId) === String(expected.id)) + S.qIdx = snapIdx; + } } catch {} } S.npOpen = true;