Skip to content

Audio Player

A small audio player: a Play and Pause button, a seek slider on the Slider Field, and the current and total time, under an optional title. Only one player plays at a time on the page.

Decision

When Use
Previewing a short sound, such as a theme song Audio Player
Video or a long playlist Not the Audio Player

Usage

import { createAudioPlayer } from './configurationpage?name=myplugin_stolon.js';

var player = createAudioPlayer({ title: 'theme.mp3', src: themeUrl });
view.querySelector('#themeHost').appendChild(player.element);

Classes: stolon-audio-player, with the stolon-playing modifier, and the parts stolon-audio-player-toggle, stolon-audio-player-body, stolon-audio-player-title, stolon-audio-player-line, and stolon-audio-player-time. The handle is { element, audio, getValue, setValue, play, pause, destroy }, where getValue() returns the source and setValue(src) loads another.

States and Modifiers

  • The button is a stolon-button with the stolon-field-sized modifier, so it is a field tall. It shows play_arrow or pause, with playText ("Play") or pauseText ("Pause") as its title and name. playing is on the root while it plays.
  • Starting a player pauses the one that was playing. onPlay runs when it starts, and onPause when it pauses or ends.
  • The player is a group named by label, the title by default, else "Audio".
  • The slider's value text reads the time, such as "0:42 of 3:05", and seekText ("Seek") names it.
  • A source that cannot load shows "Could not load the audio." in red in the title's place, which errorText replaces, and the button turns into an X, so the player keeps its height. Pressing the X, named by resetText ("Reset"), clears the error, brings the title back, and loads the audio again.
  • size is small, medium (default), or large.

Gotchas

  • Call destroy() when the page hides. It pauses the audio.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

createAudioPlayer

function createAudioPlayer(options?: AudioPlayerOptions): AudioPlayerHandle

A small audio player: a Play and Pause button, a seek slider on the Slider Field, and the current and total time, beside an optional title. Only one player plays at a time on the page, so starting one pauses the one before. The slider reads the time as its value text. When the audio cannot load, the error takes the title's place in red and the button turns into an X, so nothing on the page moves. Pressing the X clears the error, brings the title back, and loads the audio again.

Option Type What it does
src string address of the audio [optional]
title string the name shown beside the controls, such as a theme song's file name [optional]
label string the player's accessible name (default the title, else 'Audio')
playText string the play button's title and aria-label (default 'Play')
pauseText string the pause button's title and aria-label (default 'Pause')
seekText string the slider's aria-label (default 'Seek')
errorText string the text in the title's place when the audio cannot load (default 'Could not load the audio.')
resetText string the X button's title and aria-label after an error (default 'Reset')
size 'small' | 'medium' | 'large' 'small', 'medium', or 'large' for its controls (default 'medium')
onPlay (): void function() called when it starts to play [optional]
onPause (): void function() called when it pauses or ends [optional]
Handle member Type
audio HTMLAudioElement
getValue (): string
setValue (src: string | null): void
play (): void
pause (): void
destroy (): void
Class What it looks like
stolon-audio-player A small audio player: a Play and Pause button a field tall, then the title over a seek slider and the current and total time
stolon-audio-player.stolon-failed A player whose audio could not load: the error in red in the title's place, and the button an X that resets it

Example

Example Code

<!-- title: Audio Player -->
<div id="mount" style="display: grid; gap: 16px;"></div>
<script type="module">
const base = await window.__baseReady;
const mount = document.getElementById('mount');
// A short tone made in the page, so the preview needs no file.
function tone(freq, seconds) {
    const rate = 8000;
    const n = rate * seconds;
    const buf = new ArrayBuffer(44 + n * 2);
    const v = new DataView(buf);
    const s = (o, t) => [...t].forEach((c, i) => v.setUint8(o + i, c.charCodeAt(0)));
    s(0, 'RIFF'); v.setUint32(4, 36 + n * 2, true); s(8, 'WAVE'); s(12, 'fmt ');
    v.setUint32(16, 16, true); v.setUint16(20, 1, true); v.setUint16(22, 1, true);
    v.setUint32(24, rate, true); v.setUint32(28, rate * 2, true); v.setUint16(32, 2, true); v.setUint16(34, 16, true);
    s(36, 'data'); v.setUint32(40, n * 2, true);
    for (let i = 0; i < n; i++) v.setInt16(44 + i * 2, Math.sin(i / rate * freq * 2 * Math.PI) * 3000 * Math.min(1, (n - i) / 800), true);
    return URL.createObjectURL(new Blob([buf], { type: 'audio/wav' }));
}
mount.appendChild(base.createAudioPlayer({ title: 'theme-song.mp3', src: tone(440, 6) }).element);
mount.appendChild(base.createAudioPlayer({ title: 'Another theme, which pauses the first when it plays', src: tone(660, 4) }).element);
mount.appendChild(base.createAudioPlayer({ title: 'missing.mp3', src: 'missing.mp3' }).element);
</script>