From 090a145211f61c93a6d7e5ebacf04903c3899377 Mon Sep 17 00:00:00 2001 From: apt-get Date: Tue, 17 Mar 2026 00:05:44 +0100 Subject: [PATCH] add previous/next track button handling --- src/routes/streams/[stream_id]/Player.svelte | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/routes/streams/[stream_id]/Player.svelte b/src/routes/streams/[stream_id]/Player.svelte index 05ba916..beec536 100644 --- a/src/routes/streams/[stream_id]/Player.svelte +++ b/src/routes/streams/[stream_id]/Player.svelte @@ -92,6 +92,12 @@ const volumeData = localStorage.getItem('volume'); volume = volumeData ? parseFloat(volumeData) : 0.67; setVolume(volume); + + // set actions for previous/next track media buttons + if ('mediaSession' in navigator) { + navigator.mediaSession.setActionHandler('previoustrack', previousTrack); + navigator.mediaSession.setActionHandler('nexttrack', nextTrack); + } }); // update browser metadata on track changes @@ -101,6 +107,26 @@ } }); + function previousTrack() { + if (!ctx.current || ctx.songIndex == null) return; + const tracks = ctx.current.tracks; + const trackStart = tracks[ctx.songIndex][0]; + // if more than 3s into the track, restart it; otherwise go to previous + if (audio.currentTime - trackStart > 3 || ctx.songIndex === 0) { + audio.currentTime = currentTime = trackStart; + } else { + audio.currentTime = currentTime = tracks[ctx.songIndex - 1][0]; + } + } + + function nextTrack() { + if (!ctx.current || ctx.songIndex == null) return; + const tracks = ctx.current.tracks; + if (ctx.songIndex < tracks.length - 1) { + audio.currentTime = currentTime = tracks[ctx.songIndex + 1][0]; + } + } + function seek(event, bounds) { let x = event.clientX - bounds.left; return Math.min(Math.max(x / bounds.width, 0), 1);