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,