diff --git a/src/renderer/features/player/audio-player/engine/mpv-player-engine.tsx b/src/renderer/features/player/audio-player/engine/mpv-player-engine.tsx index 4bb60f062..10b2282c1 100644 --- a/src/renderer/features/player/audio-player/engine/mpv-player-engine.tsx +++ b/src/renderer/features/player/audio-player/engine/mpv-player-engine.tsx @@ -52,6 +52,8 @@ export const MpvPlayerEngine = (props: MpvPlayerEngineProps) => { const isInitializedRef = useRef(false); const hasPopulatedQueueRef = useRef(false); const isMountedRef = useRef(true); + const currentSrcRef = useRef(currentSrc); + const nextSrcRef = useRef(nextSrc); const mpvExtraParameters = useSettingsStore((store) => store.playback.mpvExtraParameters); const mpvProperties = useSettingsStore((store) => store.playback.mpvProperties); @@ -63,8 +65,6 @@ export const MpvPlayerEngine = (props: MpvPlayerEngineProps) => { const initializeMpv = async () => { const isRunning: boolean | undefined = await mpvPlayer?.isRunning(); - mpvPlayer?.stop(); - if (!isRunning) { const properties: Record = { // speed: usePlayerStore.getState().speed, @@ -81,29 +81,30 @@ export const MpvPlayerEngine = (props: MpvPlayerEngineProps) => { } else { isInitializedRef.current = true; } + + // After initialization, populate the queue if currentSrc is available + const latestCurrentSrc = currentSrcRef.current; + const latestNextSrc = nextSrcRef.current; + if (latestCurrentSrc && !hasPopulatedQueueRef.current && mpvPlayer) { + mpvPlayer.setQueue(latestCurrentSrc, latestNextSrc, true); + hasPopulatedQueueRef.current = true; + setPreviousCurrentSrc(latestCurrentSrc); + } }; initializeMpv(); return () => { isMountedRef.current = false; - mpvPlayer?.stop(); - mpvPlayer?.cleanup(); + mpvPlayer?.quit(); isInitializedRef.current = false; hasPopulatedQueueRef.current = false; }; }, [mpvExtraParameters, mpvProperties]); - // Populate mpv queue after initialization useEffect(() => { - if (!mpvPlayer || !isInitializedRef.current || hasPopulatedQueueRef.current) { - return; - } - - if (currentSrc) { - mpvPlayer.setQueue(currentSrc, nextSrc, true); - hasPopulatedQueueRef.current = true; - } + currentSrcRef.current = currentSrc; + nextSrcRef.current = nextSrc; }, [currentSrc, nextSrc]); // Update volume