AudioPlayer

Themeable HTML5 audio player β€” play/pause, scrub, volume, cover art, announced error states.

Live demo

01

Default β€” metadata + placeholder cover

SoundHelix Song 1

SoundHelix

0:00 0:00

Variant β€” looping, eager preload

SoundHelix Song 2

SoundHelix

0:00 0:00

Variant β€” custom cover snippet + broken URL (see the error surface)

🎡

Ghost Track

Nobody

0:00 0:00

The broken source above demonstrates the role="alert" error state β€” try it.

Implementation

02
AudioPlayer.svelte
<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

03

What 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 message

The 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: onerror surfaces a role="alert" message instead of a dead button.
  • Theming: every colour runs through --ap-* custom properties with a prefers-color-scheme: dark flip.
  • 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