fix: never rebuild native queue in onReady while ExoPlayer is playing

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 <noreply@anthropic.com>
This commit is contained in:
HoneyandClaude Sonnet 4.6 committed 2026-05-19 20:22:27 +02:00
1 parent 0e05740df7
commit ac4f09c8b8
1 file changed
+16 -5
+16 -5
View File
@@ -80,17 +80,28 @@ window.NSPlayer = {
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 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) {}