AudioPlayer
Themeable HTML5 audio player β play/pause, scrub, volume, cover art, announced error states.
Live demo
01Default β metadata + placeholder cover
Variant β looping, eager preload
Variant β custom cover snippet + broken URL (see the error surface)
The broken source above demonstrates the role="alert" error state β try it.
Implementation
02<script lang="ts">
import AudioPlayer from '$lib/components/AudioPlayer.svelte';
</script>
β
<AudioPlayer
src="/audio/episode-01.mp3"
title="Episode 1"
artist="The Show"
preload="metadata"
/>AudioPlayer wraps a hidden native <audio> element and derives every piece of UI state from the element's own bindings β playing is $derived(!paused), so the UI can never disagree with the media. play() rejections (autoplay policy) and load errors surface through a role="alert" region instead of failing silently. All colours run through --ap-* custom properties with a prefers-color-scheme: dark flip.
Logic explainer
03What Does It Do? (Plain English)
A drop-in replacement for the browser's native <audio controls> β a clean, themeable player with play/pause, a scrubbing timeline, volume + mute, cover art, and proper loading/error announcements. You bring an audio URL; it brings a UI that matches your site instead of the browser's default chrome.
How It Works (Pseudo-Code)
render a hidden native <audio> (bind:this, bind:paused, bind:currentTime, bind:duration)
playing = $derived(!paused) β single source of truth, no sync handlers
play/pause button β audioEl.play()/pause() (play() rejection = autoplay-block β role=alert)
scrubber (range input) β bind:value currentTime
volume slider + mute button β $effect writes volume/muted to the element
onerror β role=alert messageThe Single-Source-of-Truth Pattern
Two booleans (paused + playing) kept in sync by event handlers is the classic audio-player bug farm. This component binds only the element's own paused and derives playing from it β ending a track, pausing, or browser intervention all flow through one channel, so the UI can never disagree with the media.
State Flow Diagram
paused (initial) ββplay()βββΆ playing ββpause()/endedβββΆ paused
β β
βββ play() rejected βββΆ role=alert (autoplay blocked)
load error βββΆ role=alert (failed to load)Props Reference
| Prop | Type | Default | Description |
|---|---|---|---|
| src | string |
required | Audio URL |
| title | string |
'Unknown Track' |
Track title |
| artist | string |
'Unknown Artist' |
Artist name |
| coverArt | string |
'' |
Cover image URL |
| loop | boolean |
false |
Loop the track |
| preload | 'none' | 'metadata' | 'auto' |
'metadata' |
Native preload hint |
| cover | Snippet |
β | Overrides the whole cover-art region |
Edge Cases
- Autoplay policies:
play()rejections are caught and announced β never an unhandled rejection. - Load failure:
onerrorsurfaces arole="alert"message instead of a dead button. - Theming: every colour runs through
--ap-*custom properties with aprefers-color-scheme: darkflip. - Keyboard: buttons and both range inputs are natively operable (Tab, Enter, arrows).
Dependencies
Zero. Native HTMLAudioElement + Svelte 5 bindings only β no howler/tone/wavesurfer.
File Structure
src/lib/components/AudioPlayer.svelte β component
src/lib/components/AudioPlayer.md β this doc
src/lib/components/AudioPlayer.test.ts β 8 DOM-contract tests
src/routes/audioplayer/+page.svelte β demo