fix: ensure preloaded track exists

Part-of: <https://dev.funkwhale.audio/funkwhale/funkwhale/-/merge_requests/2346>
This commit is contained in:
Kasper Seweryn 2023-01-29 12:10:02 +01:00 committed by Marge
parent 536fa25f0e
commit 7105f5e482
2 changed files with 6 additions and 5 deletions

View File

@ -69,6 +69,8 @@ export class HTMLSound implements Sound {
this.#audio.src = source
this.#audio.preload = 'auto'
console.log('CREATED SOUND INSTANCE', this)
useEventListener(this.#audio, 'ended', () => this.#soundEndEventHook.trigger(this))
useEventListener(this.#audio, 'timeupdate', () => {
if (this.#audio.currentTime === 0) {

View File

@ -122,9 +122,8 @@ export const useTracks = createGlobalState(() => {
}, 3000, { immediate: false })
// Preload next track
const { start: startPreloadTimeout, stop: abortPreload } = useTimeoutFn(async (index) => {
const { queue } = useQueue()
const sound = await createSound(queue.value[index as number])
const { start: preload, stop: abortPreload } = useTimeoutFn(async (track: QueueTrack) => {
const sound = await createSound(track)
await sound.preload()
}, 100, { immediate: false })
@ -172,12 +171,12 @@ export const useTracks = createGlobalState(() => {
if (!hasNext.value) return
const nextTrack = queue.value[currentIndex.value + 1]
if (nextTrack && lastTrack === nextTrack) return
if (!nextTrack || lastTrack === nextTrack) return
lastTrack = nextTrack
// NOTE: Preload next track
// @ts-expect-error vueuse is wrongly typed: https://github.com/vueuse/vueuse/issues/2691
startPreloadTimeout(currentIndex.value + 1)
preload(nextTrack)
})
syncRef(track, currentTrack, {