diff --git a/front/src/api/player.ts b/front/src/api/player.ts index 98b620759..3145e75ef 100644 --- a/front/src/api/player.ts +++ b/front/src/api/player.ts @@ -90,11 +90,16 @@ export class HTMLSound implements Sound { } preload () { + console.log('CALLING PRELOAD ON', this) this.#audio.load() } dispose () { this.audioNode.disconnect() + + // Cancel any request downloading the source + this.#audio.src = '' + this.#audio.load() } async play () { diff --git a/front/src/composables/audio/tracks.ts b/front/src/composables/audio/tracks.ts index 09d2da72e..01e1965f3 100644 --- a/front/src/composables/audio/tracks.ts +++ b/front/src/composables/audio/tracks.ts @@ -19,7 +19,10 @@ const ALLOWED_PLAY_TYPES: (CanPlayTypeResult | undefined)[] = ['maybe', 'probabl const AUDIO_ELEMENT = document.createElement('audio') const soundPromises = new Map>() -const soundCache = useLRUCache({ max: 10 }) +const soundCache = useLRUCache({ + max: 10, + dispose: (sound) => sound.dispose() +}) export const fetchTrackSources = async (id: number): Promise => { const { uploads } = await axios.get(`tracks/${id}/`) @@ -124,6 +127,7 @@ export const useTracks = createGlobalState(() => { // Preload next track const { start: preload, stop: abortPreload } = useTimeoutFn(async (track: QueueTrack) => { const sound = await createSound(track) + sound.__track = track await sound.preload() }, 100, { immediate: false }) @@ -153,7 +157,7 @@ export const useTracks = createGlobalState(() => { await sound.play() } } - + const currentTrack = ref() // NOTE: We want to have it called only once, hence we're using createGlobalState