Skip to content

Commit

Permalink
Additional player debug information
Browse files Browse the repository at this point in the history
  • Loading branch information
martpie committed Jan 4, 2025
1 parent c0551cd commit 8badc2d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
24 changes: 12 additions & 12 deletions src/components/PlayerEvents.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { getCurrentWindow } from '@tauri-apps/api/window';
import { error as logError } from '@tauri-apps/plugin-log';
import { sendNotification } from '@tauri-apps/plugin-notification';
import { useEffect } from 'react';

import { error } from '@tauri-apps/plugin-log';
import config from '../lib/config';
import { getCover } from '../lib/cover';
import player from '../lib/player';
import { logAndNotifyError } from '../lib/utils';
import { usePlayerAPI } from '../stores/usePlayerStore';
import { useToastsAPI } from '../stores/useToastsStore';

const AUDIO_ERRORS = {
aborted: 'The video playback was aborted.',
corrupt: 'The audio playback was aborted due to a corruption problem.',
notFound:
'The track file could not be found. It may be due to a file move, an unmounted partition or missing rights.',
'The track file could not be loaded. It may be due to a file move, an unmounted partition or missing rights.',
unknown: 'An unknown error occurred.',
};

Expand All @@ -24,36 +23,37 @@ const AUDIO_ERRORS = {
*/
function PlayerEvents() {
const playerAPI = usePlayerAPI();
const toastsAPI = useToastsAPI();

useEffect(() => {
function handleAudioError(e: ErrorEvent) {
playerAPI.stop();

error(e.error);
error(e.message);

const element = e.target as HTMLAudioElement;

if (element) {
const { error } = element;

if (!error) return;

let errorMessage;

switch (error.code) {
case error.MEDIA_ERR_ABORTED:
toastsAPI.add('warning', AUDIO_ERRORS.aborted);
errorMessage = AUDIO_ERRORS.aborted;
break;
case error.MEDIA_ERR_DECODE:
toastsAPI.add('danger', AUDIO_ERRORS.corrupt);
errorMessage = AUDIO_ERRORS.corrupt;
break;
case error.MEDIA_ERR_SRC_NOT_SUPPORTED:
toastsAPI.add('danger', AUDIO_ERRORS.notFound);
errorMessage = AUDIO_ERRORS.notFound;
break;
default:
toastsAPI.add('danger', AUDIO_ERRORS.unknown);
errorMessage = AUDIO_ERRORS.unknown;
break;
}

logAndNotifyError(errorMessage);
logError(player.getDebug());
}
}

Expand Down Expand Up @@ -94,7 +94,7 @@ function PlayerEvents() {
player.getAudio().removeEventListener('error', handleAudioError);
player.getAudio().removeEventListener('ended', playerAPI.next);
};
}, [toastsAPI, playerAPI]);
}, [playerAPI]);

return null;
}
Expand Down
11 changes: 11 additions & 0 deletions src/lib/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ class Player {
isPaused() {
return this.audio.paused;
}

getDebug(): string {
return `
Player Debug Information:
- mode: ${this.playbackMode}
- internal src: ${this.audio.src}
- external src: ${this.track?.path}
- current time: ${this.audio.currentTime}
- playback rate: ${this.audio.playbackRate}
- volume: ${this.audio.volume}`;
}
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/stores/usePlayerStore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { error } from '@tauri-apps/plugin-log';
import debounce from 'lodash-es/debounce';
import type { StateCreator } from 'zustand';
import { persist } from 'zustand/middleware';
Expand Down Expand Up @@ -107,7 +108,9 @@ const usePlayerStore = createPlayerStore<PlayerState>((set, get) => ({
const track = queue[queuePosition];

await player.setTrack(track);
await player.play().catch(logAndNotifyError);
await player
.play()
.catch((err) => logAndNotifyError(err, undefined, false, true));

let queueCursor = queuePosition; // Clean that variable mess later

Expand Down

0 comments on commit 8badc2d

Please sign in to comment.