add previous/next track button handling
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user