Add mpv load error notification

- Add retry limit on error
This commit is contained in:
jeffvli 2023-05-20 20:19:41 -07:00
parent 1f36978bb9
commit 0320fe6dcc
4 changed files with 47 additions and 13 deletions

View File

@ -1,5 +1,5 @@
import { ipcMain } from 'electron';
import { getMpvInstance } from '../../../main';
import { getMainWindow, getMpvInstance } from '../../../main';
import { PlayerData } from '/@/renderer/store';
declare module 'node-mpv';
@ -60,21 +60,28 @@ ipcMain.on('player-set-queue', async (_event, data: PlayerData) => {
}
let complete = false;
let tryAttempts = 0;
while (!complete) {
try {
if (data.queue.current) {
await getMpvInstance()?.load(data.queue.current.streamUrl, 'replace');
}
if (data.queue.next) {
await getMpvInstance()?.load(data.queue.next.streamUrl, 'append');
}
if (tryAttempts > 3) {
getMainWindow()?.webContents.send('renderer-player-error', 'Failed to load song');
complete = true;
} catch (err) {
console.error(err);
await wait(500);
} else {
try {
if (data.queue.current) {
await getMpvInstance()?.load(data.queue.current.streamUrl, 'replace');
}
if (data.queue.next) {
await getMpvInstance()?.load(data.queue.next.streamUrl, 'append');
}
complete = true;
} catch (err) {
console.error(err);
tryAttempts += 1;
await wait(500);
}
}
}
});

View File

@ -421,6 +421,12 @@ ipcMain.on(
},
);
ipcMain.on('player-quit', async () => {
mpvInstance?.stop();
mpvInstance?.quit();
mpvInstance = null;
});
// Must duplicate with the one in renderer process settings.store.ts
enum BindingActions {
GLOBAL_SEARCH = 'globalSearch',

View File

@ -134,6 +134,10 @@ const rendererQuit = (cb: (event: IpcRendererEvent) => void) => {
ipcRenderer.on('renderer-player-quit', cb);
};
const rendererError = (cb: (event: IpcRendererEvent, data: string) => void) => {
ipcRenderer.on('renderer-player-error', cb);
};
export const mpvPlayer = {
autoNext,
currentTime,
@ -157,6 +161,7 @@ export const mpvPlayer = {
export const mpvPlayerListener = {
rendererAutoNext,
rendererCurrentTime,
rendererError,
rendererNext,
rendererPause,
rendererPlay,

View File

@ -15,6 +15,7 @@ import { usePlayerType, useSettingsStore } from '/@/renderer/store/settings.stor
import { useScrobble } from '/@/renderer/features/player/hooks/use-scrobble';
import debounce from 'lodash/debounce';
import { QueueSong } from '/@/renderer/api/types';
import { toast } from '/@/renderer/components';
const mpvPlayer = isElectron() ? window.electron.mpvPlayer : null;
const mpvPlayerListener = isElectron() ? window.electron.mpvPlayerListener : null;
@ -527,6 +528,15 @@ export const useCenterControls = (args: { playersRef: any }) => {
mpvPlayer.quit();
}, []);
const handleError = useCallback(
(message: string) => {
toast.error({ id: 'mpv-error', message, title: 'An error occurred during playback' });
pause();
mpvPlayer.pause();
},
[pause],
);
useEffect(() => {
if (isElectron()) {
mpvPlayerListener.rendererPlayPause(() => {
@ -572,6 +582,10 @@ export const useCenterControls = (args: { playersRef: any }) => {
mpvPlayerListener.rendererToggleRepeat(() => {
handleToggleRepeat();
});
mpvPlayerListener.rendererError((_event: any, message: string) => {
handleError(message);
});
}
return () => {
@ -586,10 +600,12 @@ export const useCenterControls = (args: { playersRef: any }) => {
ipc?.removeAllListeners('renderer-player-quit');
ipc?.removeAllListeners('renderer-player-toggle-shuffle');
ipc?.removeAllListeners('renderer-player-toggle-repeat');
ipc?.removeAllListeners('renderer-player-error');
};
}, [
autoNext,
handleAutoNext,
handleError,
handleNextTrack,
handlePause,
handlePlay,