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) {}