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
This commit is contained in:
jeffvli 2023-03-09 12:45:13 -08:00
parent 7f9de4b180
commit 275d68ec5b
2 changed files with 44 additions and 22 deletions

View File

@ -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

View File

@ -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,