global player pt. 6 attempt to skip tracks

This commit is contained in:
2026-02-06 01:59:59 -08:00
parent a85441faa7
commit e91ea1761f

View File

@@ -61,13 +61,11 @@
pause: () => audioEl?.pause(), pause: () => audioEl?.pause(),
next: () => { next: () => {
next(); next();
syncAudioToCurrentTrack(); void syncAndAutoplay();
void audioEl?.play();
}, },
prev: () => { prev: () => {
prev(currentTime); prev(currentTime);
syncAudioToCurrentTrack(); void syncAndAutoplay();
void audioEl?.play();
}, },
seekTo: (t) => { seekTo: (t) => {
if (!audioEl) return; if (!audioEl) return;
@@ -97,6 +95,35 @@
audioEl.volume = snap.volume; audioEl.volume = snap.volume;
} }
async function syncAndAutoplay() {
if (!audioEl) return;
syncAudioToCurrentTrack();
// If the source changed, playback may not start immediately on some browsers
// until we wait for the new media to be ready.
if (audioEl.readyState < 2) {
await new Promise<void>((resolve) => {
const el = audioEl;
if (!el) {
resolve();
return;
}
const onCanPlay = () => {
el.removeEventListener("canplay", onCanPlay);
resolve();
};
el.addEventListener("canplay", onCanPlay, { once: true });
});
}
try {
await audioEl.play();
} catch {
// Autoplay may be blocked; user gesture will still allow manual play.
}
}
function onAudioPlay() { function onAudioPlay() {
isPlaying = true; isPlaying = true;
media.setPlaybackState("playing"); media.setPlaybackState("playing");
@@ -125,8 +152,7 @@
} }
function onAudioEnded() { function onAudioEnded() {
next(); next();
syncAudioToCurrentTrack(); void syncAndAutoplay();
if (snap.currentTrack) void audioEl?.play();
} }
function formatTime(seconds: number) { function formatTime(seconds: number) {
@@ -222,8 +248,7 @@
disabled={!canPrev} disabled={!canPrev}
onclick={() => { onclick={() => {
prev(currentTime); prev(currentTime);
syncAudioToCurrentTrack(); void syncAndAutoplay();
void audioEl?.play();
}} }}
> >
Prev Prev
@@ -248,8 +273,7 @@
disabled={!canNext} disabled={!canNext}
onclick={() => { onclick={() => {
next(); next();
syncAudioToCurrentTrack(); void syncAndAutoplay();
void audioEl?.play();
}} }}
> >
Next Next
@@ -307,8 +331,7 @@
type="button" type="button"
onclick={() => { onclick={() => {
jumpToTrack(t.id); jumpToTrack(t.id);
syncAudioToCurrentTrack(); void syncAndAutoplay();
void audioEl?.play();
}} }}
> >
{#if snap.currentIndex === i} {#if snap.currentIndex === i}
@@ -387,8 +410,7 @@
disabled={!canPrev} disabled={!canPrev}
onclick={() => { onclick={() => {
prev(currentTime); prev(currentTime);
syncAudioToCurrentTrack(); void syncAndAutoplay();
void audioEl?.play();
}} }}
> >
Prev Prev
@@ -413,8 +435,7 @@
disabled={!canNext} disabled={!canNext}
onclick={() => { onclick={() => {
next(); next();
syncAudioToCurrentTrack(); void syncAndAutoplay();
void audioEl?.play();
}} }}
> >
Next Next
@@ -455,8 +476,7 @@
type="button" type="button"
onclick={() => { onclick={() => {
jumpToTrack(t.id); jumpToTrack(t.id);
syncAudioToCurrentTrack(); void syncAndAutoplay();
void audioEl?.play();
}} }}
> >
{#if snap.currentIndex === i} {#if snap.currentIndex === i}