From ac4f09c8b88800171583f444d7f7ac65dcb5d806 Mon Sep 17 00:00:00 2001 From: Honey Date: Tue, 19 May 2026 20:22:27 +0200 Subject: [PATCH] fix: never rebuild native queue in onReady while ExoPlayer is playing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When onReady fires during MediaController reconnect (fragment navigation, activity resume), calling setQueue restarts the current song from pos 0. Guard: if native isPlaying=true, only re-sync JS UI — never touch the queue. Only push a new queue when native is idle/empty (cold start). Co-Authored-By: Claude Sonnet 4.6 --- src/player.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/player.js b/src/player.js index 32bbf2a..00a2ea0 100644 --- a/src/player.js +++ b/src/player.js @@ -79,18 +79,29 @@ window.NSPlayer = { try { const raw = window.NSBridge.getState(); const state = raw ? (JSON.parse(raw) || {}) : {}; - const nativeLen = state.queueLength || 0; - if (nativeLen > 0 && nativeLen === S.queue.length) { - // Native still has the same queue — just re-sync UI state. + const nativeLen = state.queueLength || 0; + const nativePlaying = state.isPlaying || false; + if (nativeLen > 0 && nativePlaying) { + // Native is actively playing — never rebuild the queue, it would restart the song. + // Just re-sync JS state and UI from whatever native reports. S.qIdx = state.currentIndex ?? S.qIdx; - _nativePlaying = state.isPlaying || false; + _nativePlaying = nativePlaying; _nativePos = state.positionMs || 0; const s = S.queue[S.qIdx]; if (s) { updatePlayerUI(s, true); onNpSongChange(s.id); } setPlayIcon(_nativePlaying); renderQueue(); - } else { - // Native queue is empty or stale — push JS state (restored from localStorage). + } else if (nativeLen > 0 && nativeLen === S.queue.length) { + // Paused but queue matches — re-sync UI state only. + S.qIdx = state.currentIndex ?? S.qIdx; + _nativePlaying = false; + _nativePos = state.positionMs || 0; + const s = S.queue[S.qIdx]; + if (s) { updatePlayerUI(s, true); onNpSongChange(s.id); } + setPlayIcon(false); + renderQueue(); + } else if (!nativePlaying) { + // Native is idle/empty — safe to push JS state (e.g. after cold start). window.NSBridge.setQueue(JSON.stringify(buildTrackList()), S.qIdx, false); } } catch (e) {}