From 275d68ec5b0887dee95b8a3083c79046ea794df8 Mon Sep 17 00:00:00 2001 From: jeffvli Date: Thu, 9 Mar 2023 12:45:13 -0800 Subject: [PATCH] Fix mpv stopping after first playback - On startup, the first time a song is played, mpv will stop after playback - This adds a loop to the queue handler to automatically retry when failing to add to the queue --- src/main/features/core/player/index.ts | 37 +++++++++++++++---- .../player/hooks/use-handle-playqueue-add.ts | 29 +++++++-------- 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/main/features/core/player/index.ts b/src/main/features/core/player/index.ts index 5afcdfe5..360d5099 100644 --- a/src/main/features/core/player/index.ts +++ b/src/main/features/core/player/index.ts @@ -4,6 +4,19 @@ import { PlayerData } from '/@/renderer/store'; declare module 'node-mpv'; +function wait(timeout: number) { + return new Promise((resolve) => { + setTimeout(() => { + resolve('resolved'); + }, timeout); + }); +} + +ipcMain.on('player-start', async () => { + await mpv.load('./dummy.mp3', 'replace'); + await mpv.play(); +}); + // Starts the player ipcMain.on('player-play', async () => { await mpv.play(); @@ -47,15 +60,25 @@ ipcMain.on('player-set-queue', async (_event, data: PlayerData) => { return; } - if (data.queue.current) { - await mpv.load(data.queue.current.streamUrl, 'replace'); - } + let complete = false; - if (data.queue.next) { - await mpv.load(data.queue.next.streamUrl, 'append'); - } + while (!complete) { + try { + if (data.queue.current) { + await mpv.load(data.queue.current.streamUrl, 'replace'); + } - await mpv.play(); + if (data.queue.next) { + await mpv.load(data.queue.next.streamUrl, 'append'); + } + + await mpv.play(); + complete = true; + } catch (err) { + console.error(err); + await wait(500); + } + } }); // Replaces the queue in position 1 to the given data diff --git a/src/renderer/features/player/hooks/use-handle-playqueue-add.ts b/src/renderer/features/player/hooks/use-handle-playqueue-add.ts index c279c765..60157e51 100644 --- a/src/renderer/features/player/hooks/use-handle-playqueue-add.ts +++ b/src/renderer/features/player/hooks/use-handle-playqueue-add.ts @@ -168,6 +168,20 @@ export const useHandlePlayQueueAdd = () => { if (!songs) return toast.warn({ message: 'No songs found' }); const playerData = usePlayerStore.getState().actions.addToQueue(songs, options.play); + + if (playerType === PlaybackType.LOCAL) { + if (options.play === Play.NEXT || options.play === Play.LAST) { + mpvPlayer.setQueueNext(playerData); + } + + if (options.play === Play.NOW) { + mpvPlayer.setQueue(playerData); + mpvPlayer.play(); + } + } + + play(); + mpris?.updateSong({ currentTime: usePlayerStore.getState().current.time, repeat: usePlayerStore.getState().repeat, @@ -176,21 +190,6 @@ export const useHandlePlayQueueAdd = () => { status: 'Playing', }); - if (options.play === Play.NEXT || options.play === Play.LAST) { - if (playerType === PlaybackType.LOCAL) { - mpvPlayer.setQueueNext(playerData); - } - } - - if (options.play === Play.NOW) { - if (playerType === PlaybackType.LOCAL) { - mpvPlayer.setQueue(playerData); - mpvPlayer.play(); - } - - play(); - } - // if (fetchId) { // toast.update({ // autoClose: 1000,