autoplay-retry
All checks were successful
check / frontend (push) Successful in 37s
check / server (push) Successful in 1m3s

This commit is contained in:
Pen Anderson 2026-05-27 07:01:46 -05:00
parent 7bb21c245c
commit c09b5f4308

View file

@ -12,6 +12,7 @@
let duration = $state(0); let duration = $state(0);
let loading = $state(false); let loading = $state(false);
let errored = $state(false); let errored = $state(false);
let intent = $state(false);
function toggle() { function toggle() {
if (errored) { if (errored) {
@ -20,9 +21,11 @@
} }
if (!audio) return; if (!audio) return;
if (audio.paused) { if (audio.paused) {
intent = true;
if (audio.readyState < 3) loading = true; if (audio.readyState < 3) loading = true;
audio.play().catch(() => undefined); audio.play().catch(() => undefined);
} else { } else {
intent = false;
audio.pause(); audio.pause();
} }
} }
@ -30,6 +33,7 @@
function retry() { function retry() {
if (!audio) return; if (!audio) return;
errored = false; errored = false;
intent = true;
loading = true; loading = true;
audio.load(); audio.load();
audio.play().catch(() => undefined); audio.play().catch(() => undefined);
@ -47,7 +51,8 @@
$effect(() => { $effect(() => {
if (!autoplay || !audio) return; if (!autoplay || !audio) return;
audio.play().catch(() => undefined); intent = true;
audio.play().catch(() => { intent = false; });
}); });
function fmt(s: number) { function fmt(s: number) {
@ -119,7 +124,7 @@
bind:duration bind:duration
onloadstart={() => { errored = false; }} onloadstart={() => { errored = false; }}
oncanplay={() => { loading = false; }} oncanplay={() => { loading = false; }}
onwaiting={() => { if (!paused) loading = true; }} onwaiting={() => { if (intent) loading = true; }}
onplaying={onPlaying} onplaying={onPlaying}
onerror={() => { loading = false; errored = true; }} onerror={() => { loading = false; errored = true; }}
onended={() => { paused = true; }} onended={() => { paused = true; }}