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
+17 -6
+17 -6
View File
@@ -79,18 +79,29 @@ window.NSPlayer = {
try { try {
const raw = window.NSBridge.getState(); const raw = window.NSBridge.getState();
const state = raw ? (JSON.parse(raw) || {}) : {}; const state = raw ? (JSON.parse(raw) || {}) : {};
const nativeLen = state.queueLength || 0; const nativeLen = state.queueLength || 0;
if (nativeLen > 0 && nativeLen === S.queue.length) { const nativePlaying = state.isPlaying || false;
// Native still has the same queue — just re-sync UI state. 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; S.qIdx = state.currentIndex ?? S.qIdx;
_nativePlaying = state.isPlaying || false; _nativePlaying = nativePlaying;
_nativePos = state.positionMs || 0; _nativePos = state.positionMs || 0;
const s = S.queue[S.qIdx]; const s = S.queue[S.qIdx];
if (s) { updatePlayerUI(s, true); onNpSongChange(s.id); } if (s) { updatePlayerUI(s, true); onNpSongChange(s.id); }
setPlayIcon(_nativePlaying); setPlayIcon(_nativePlaying);
renderQueue(); renderQueue();
} else { } else if (nativeLen > 0 && nativeLen === S.queue.length) {
// Native queue is empty or stale — push JS state (restored from localStorage). // 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); window.NSBridge.setQueue(JSON.stringify(buildTrackList()), S.qIdx, false);
} }
} catch (e) {} } catch (e) {}