From c50ecdd73c4021e3bcd7865262835dc93141fdb7 Mon Sep 17 00:00:00 2001 From: Izan Gil <66965250+SrIzan10@users.noreply.github.com> Date: Sun, 20 Apr 2025 18:04:17 +0200 Subject: [PATCH] fix: ios playback not working properly --- src/lib/components/app/daemon.svelte | 63 +++++++++++++++++++--------- 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/src/lib/components/app/daemon.svelte b/src/lib/components/app/daemon.svelte index ce592f9..96f5bba 100644 --- a/src/lib/components/app/daemon.svelte +++ b/src/lib/components/app/daemon.svelte @@ -5,6 +5,7 @@ // svelte-ignore non_reactive_update let audioElement: HTMLAudioElement; + let isTransitioning = false; function togglePlayback(play: boolean) { if (!audioElement) return; @@ -26,6 +27,47 @@ togglePlayback(state.isPlaying); }; + // ios safari requires a user gesture to play audio + // please send help + function handleSongEnd() { + if (isTransitioning) return; + + isTransitioning = true; + state.currentSong = null; + state.currentTime = 0; + + state.songQueue.shift(); + if (state.songQueue.length > 0) { + state.currentSong = state.songQueue[0]; + state.duration = state.currentSong.duration; + + setTimeout(() => { isTransitioning = false; }, 500); + } else { + getStationSongs(state.currentStation!).then((songs) => { + if (songs) { + state.songQueue = songs; + state.currentSong = state.songQueue[0]; + state.duration = state.currentSong.duration; + } else { + state.error = 'Failed to load songs.'; + } + + setTimeout(() => { isTransitioning = false; }, 500); + }); + } + } + + function checkTimeAndPrepareNextSong() { + if (!audioElement || !state.currentSong || isTransitioning) return; + + // if near the end, prepare for next song + if (audioElement.duration > 0 && + audioElement.currentTime > 0 && + audioElement.duration - audioElement.currentTime < 1.5) { + handleSongEnd(); + } + } + onMount(async () => { const data = await getGeneralData(); state.presets = data.presets; @@ -128,25 +170,8 @@ src={`https://stream.chillhop.com/mp3/${state.currentSong!.fileId}`} autoplay volume={state.volume} - onended={async () => { - state.currentSong = null; - state.currentTime = 0; - - state.songQueue.shift(); - if (state.songQueue.length > 0) { - state.currentSong = state.songQueue[0]; - state.duration = state.currentSong.duration; - } else { - const stationSongs = await getStationSongs(state.currentStation!); - if (stationSongs) { - state.songQueue = stationSongs; - state.currentSong = state.songQueue[0]; - state.duration = state.currentSong.duration; - } else { - state.error = 'Failed to load songs.'; - } - } - }} + ontimeupdate={checkTimeAndPrepareNextSong} + onended={handleSongEnd} class="hidden" > {/if}