Skip to main content

Playback Controls

Waveform Playlist provides both pre-built UI components and hooks for controlling audio playback.

Button Components​

Import and use the built-in button components:

import {
PlayButton,
PauseButton,
StopButton,
RewindButton,
FastForwardButton,
} from '@waveform-playlist/browser';

function Controls() {
return (
<div>
<RewindButton />
<PlayButton />
<PauseButton />
<StopButton />
<FastForwardButton />
</div>
);
}

These buttons automatically connect to the playlist context and update their state accordingly. Play is disabled while playing; Pause and Stop are disabled unless playing. Rewind, Fast Forward and the two skip buttons call seekTo, which holds the position between 0 and the end of the audio (a finite time past the end is kept with indefinitePlayback or during a recording session; Fast Forward goes to the end) and, during playback, restarts playback at the new position.

Which controls work under which provider​

The controls read the nearest playlist provider. They are exports of @waveform-playlist/browser, except where the table names another package.

Works underComponents
Either providerPlayButton, PauseButton, StopButton, RewindButton, FastForwardButton, SkipBackwardButton, SkipForwardButton, ClearAllButton, AudioPosition, TimeFormatSelect, SelectionTimeInputs, AutomaticScrollCheckbox, and, from @waveform-playlist/annotations, ContinuousPlayCheckbox, LinkEndpointsCheckbox, EditableCheckbox and DownloadAnnotationsButton
MediaElementProvider onlyPlaybackRateSelect
WebAudioProvider onlyLoopButton, SetLoopRegionButton, ZoomInButton, ZoomOutButton, MasterVolumeControl, KeyboardShortcuts, ExportWavButton (from @waveform-playlist/browser/tone)

A control used under a provider that does not serve it throws, for example "useLoop needs WebAudioProvider, and the nearest playlist provider here is MediaElementProvider, which does not provide it".

Custom Playback Controls​

For custom UI, use usePlaybackControls() and useIsPlaying(). Both work under either provider:

import { usePlaybackControls, useIsPlaying } from '@waveform-playlist/browser';

function CustomControls() {
const { play, pause, stop } = usePlaybackControls();
const { isPlaying } = useIsPlaying();

return (
<div>
<button onClick={() => play()} disabled={isPlaying}>
Play
</button>
<button onClick={pause} disabled={!isPlaying}>
Pause
</button>
<button onClick={stop}>
Stop
</button>
<span>Status: {isPlaying ? 'Playing' : 'Stopped'}</span>
</div>
);
}

Seeking​

Click-to-Seek​

By default, clicking on the waveform seeks to that position, and dragging selects a range.

During playback nothing happens until the mouse button is released:

  • A click moves playback to the clicked position.
  • A drag is highlighted as it is drawn while playback carries on; on release the range becomes the selection and is played from its start to its end (with looping on under WebAudioProvider: open-ended from its start).
  • With automatic scroll on, no range can be drawn during playback (the waveform moves under the pointer): a press and its release are one seek, to where the press landed. Switch automatic scroll off to select a range while playing.

Programmatic Seeking​

import { usePlaybackControls } from '@waveform-playlist/browser';

function SeekControls() {
const { seekTo } = usePlaybackControls();

return (
<div>
<button onClick={() => seekTo(0)}>Go to Start</button>
<button onClick={() => seekTo(30)}>Go to 0:30</button>
<button onClick={() => seekTo(60)}>Go to 1:00</button>
</div>
);
}

Current Position​

AudioPosition Component​

Display the current playback position:

import { AudioPosition } from '@waveform-playlist/browser';

function PositionDisplay() {
return <AudioPosition />;
}
// Displays the position only, in the provider's time format
// (default hh:mm:ss.uuu), for example "00:00:08.000"

Programmatic Access​

Access the resting cursor position from useCurrentTime() and duration from useTimeline() (under MediaElementProvider, from useMediaElementTrack()). currentTime changes on a seek, a pause and a stop; it does not advance during playback. For a display that runs during playback, use <AudioPosition /> or register a frame callback with usePlaybackRefs().

import { useCurrentTime, useTimeline } from '@waveform-playlist/browser';

function CustomPosition() {
const { currentTime } = useCurrentTime();
const { duration } = useTimeline();

const formatTime = (seconds: number) => {
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins}:${secs.toString().padStart(2, '0')}`;
};

return (
<span>
{formatTime(currentTime)} / {formatTime(duration)}
</span>
);
}

Time Format Selection​

Allow users to choose their preferred time format:

import { TimeFormatSelect } from '@waveform-playlist/browser';

function FormatSelector() {
return <TimeFormatSelect />;
}

Available formats:

  • seconds - whole seconds: 3726
  • thousandths - seconds with three decimals: 3725.500
  • hh:mm:ss - 01:02:06
  • hh:mm:ss.u - 01:02:05.5
  • hh:mm:ss.uu - 01:02:05.50
  • hh:mm:ss.uuu - 01:02:05.500

Continuous Play​

Continuous play is an annotation option, not a loop. Off: playing an annotation stops at that annotation's end. On: playback runs on through the following annotations. For looping see LoopButton.

The checkbox is an export of @waveform-playlist/annotations. It needs a playlist provider above it (either one) and does not need <AnnotationProvider>:

import { ContinuousPlayCheckbox } from '@waveform-playlist/annotations';

function ContinuousPlayControl() {
return <ContinuousPlayCheckbox />;
}

Or control it programmatically:

import { useAnnotationOptions, usePlaybackControls } from '@waveform-playlist/browser';

function ContinuousPlayToggle() {
const { continuousPlay } = useAnnotationOptions();
const { setContinuousPlay } = usePlaybackControls();

return (
<label>
<input
type="checkbox"
checked={continuousPlay}
onChange={(e) => setContinuousPlay(e.target.checked)}
/>
Continuous Play
</label>
);
}

Automatic Scroll​

Keep the playhead visible during playback. While it is on, a drag on the waveform during playback does not select a range (see Click-to-Seek):

import { AutomaticScrollCheckbox } from '@waveform-playlist/browser';

function ScrollControl() {
return <AutomaticScrollCheckbox />;
}

Playback Speed​

MediaElementProvider only. PlaybackRateSelect is a dropdown of speeds that reads and sets the provider's playback rate:

import { PlaybackRateSelect } from '@waveform-playlist/browser';

function SpeedControl() {
return <PlaybackRateSelect />;
}

// Or choose the speeds to offer:
<PlaybackRateSelect rates={[0.75, 1, 1.5]} />;

Without rates it offers 0.5, 0.75, 1, 1.25, 1.5 and 2. For a custom control, read usePlaybackRate():

import { usePlaybackRate } from '@waveform-playlist/browser';

function SpeedButtons() {
const { playbackRate, setPlaybackRate } = usePlaybackRate();
// setPlaybackRate clamps to 0.5–2
return <button onClick={() => setPlaybackRate(1.5)}>{playbackRate}×</button>;
}

Loop Playback​

WebAudioProvider only; there is no loop under MediaElementProvider.

Waveform Playlist uses an Audacity-style loop system with a separate loop region from the selection. This allows you to:

  • Define a loop region independently from your playback selection
  • Start playback before the loop region - it will loop when the cursor enters and reaches the end of the region
  • Toggle looping on/off without losing the loop region
  • Create and adjust loop regions visually in the timescale area

The loop region is displayed only in the timescale (ruler) area, keeping the tracks area clean for waveform visualization and selection.

Two-Button Workflow​

  1. SetLoopRegionButton - Creates a loop region from the current selection (or clears it)
  2. LoopButton - Enables/disables the looping behavior
import { SetLoopRegionButton, LoopButton } from '@waveform-playlist/browser';

function LoopControls() {
return (
<div>
<SetLoopRegionButton />
<LoopButton />
</div>
);
}

SetLoopRegionButton​

Creates a loop region from the current selection.

<SetLoopRegionButton />

Behavior:

  • Disabled when no selection exists and no loop region is set
  • Shows "Set Loop" when no loop region exists
  • Shows "Clear Loop" when a loop region is set
  • Click to set loop region from selection, or clear existing loop region

LoopButton​

Toggles the looping behavior.

<LoopButton />

Behavior:

  • Shows "Loop Off" when disabled, "Loop On" when enabled
  • If no loop region exists when enabling, creates a default loop region (first 10s or 25% of duration)
  • When enabled, playback loops when the cursor enters and reaches the end of the loop region

Programmatic Control​

import { useLoop, useSelection, useEditorControls } from '@waveform-playlist/browser';

function CustomLoopControls() {
const { isLoopEnabled, loopStart, loopEnd } = useLoop();
const { selectionStart, selectionEnd } = useSelection();
const { setLoopEnabled, setLoopRegionFromSelection, clearLoopRegion } = useEditorControls();

const hasSelection = selectionStart !== selectionEnd && selectionEnd > selectionStart;
const hasLoopRegion = loopStart !== loopEnd && loopEnd > loopStart;

return (
<div>
<button
onClick={hasLoopRegion ? clearLoopRegion : setLoopRegionFromSelection}
disabled={!hasSelection && !hasLoopRegion}
>
{hasLoopRegion ? 'Clear Loop' : 'Set Loop'}
</button>
<button
onClick={() => setLoopEnabled(!isLoopEnabled)}
disabled={!hasLoopRegion}
>
{isLoopEnabled ? 'Loop On' : 'Loop Off'}
</button>
</div>
);
}

Complete Loop Example​

import {
WebAudioProvider,
Waveform,
PlayButton,
PauseButton,
StopButton,
SetLoopRegionButton,
LoopButton,
SelectionTimeInputs,
useCreateArrangement,
} from '@waveform-playlist/browser';
import { audioTrack } from '@waveform-playlist/core';

function LoopExample() {
const arrangement = useCreateArrangement(() => [
audioTrack({ src: '/audio/track.mp3', name: 'Track' }),
]);

return (
<WebAudioProvider arrangement={arrangement}>
<div style={{ display: 'flex', gap: '0.5rem', marginBottom: '1rem' }}>
<PlayButton />
<PauseButton />
<StopButton />
<SetLoopRegionButton />
<LoopButton />
</div>
<SelectionTimeInputs />
<Waveform />
</WebAudioProvider>
);
}

Workflow:

  1. Click and drag on the waveform to create a selection
  2. Click "Set Loop" to create a loop region from the selection (or drag in the timescale)
  3. Click "Loop On" to enable looping
  4. Press Play - when the cursor reaches the loop region, it will loop

Selection + Loop Interaction:

  • With a selection and no loop: Play starts at selection start and stops at selection end
  • With a selection and loop enabled: Play starts at selection start and continues, looping when it reaches the loop region

Selection Playback​

Play only the selected region:

import { useSelection, usePlaybackControls } from '@waveform-playlist/browser';

function PlaySelection() {
const { selectionStart, selectionEnd } = useSelection();
const { play } = usePlaybackControls();

const playSelectedRegion = () => {
if (selectionStart !== selectionEnd) {
play(selectionStart, selectionEnd - selectionStart);
}
};

return (
<button onClick={playSelectedRegion} disabled={selectionStart === selectionEnd}>
Play Selection
</button>
);
}

Master Volume​

Control the overall output volume:

import { MasterVolumeControl } from '@waveform-playlist/browser';

function VolumeControl() {
return <MasterVolumeControl />;
}

Or with a custom UI:

import { useMixer, useEditorControls } from '@waveform-playlist/browser';

function CustomVolumeSlider() {
const { masterVolume } = useMixer();
const { setMasterVolume } = useEditorControls();

return (
<input
type="range"
min="0"
max="1"
step="0.01"
value={masterVolume}
onChange={(e) => setMasterVolume(parseFloat(e.target.value))}
/>
);
}

Keyboard Shortcuts​

<KeyboardShortcuts> works under WebAudioProvider only. Nothing in the library installs keyboard shortcuts under MediaElementProvider.

Waveform Playlist supports common keyboard shortcuts:

KeyAction
SpacePlay/Pause toggle. Starts playback exactly as the Play button does: the highlighted selection if there is one (and looping is off), otherwise from the cursor. If the cursor is inside the selection (playback was paused there, for example), both resume from the cursor and stop at the selection's end; Stop, or the selection finishing, returns the cursor to the selection's start
EscapeStop. During a drag it only cancels the drag (see below). With annotations on: while playing it stops and the selected annotation stays selected; not playing, with an annotation selected, it deselects the annotation instead.
0Rewind to start
SSplit the clip at the playhead on the selected track (clipSplitting)
Cmd/Ctrl+ZUndo (undo). Does nothing while a clip or track drag is in progress.
Cmd/Ctrl+Shift+ZRedo (undo). Does nothing while a clip or track drag is in progress.

Space, Escape and 0 come with playback; S and the undo keys with the props named in the table. Space, Escape, 0 and S do not run with Ctrl, Cmd or Alt held: Cmd+S, Ctrl+0 or Alt+Space stay the browser's. Space and S also do nothing with Shift; Escape still stops with Shift held. Undo and redo are exactly Ctrl+Z / Cmd+Z (redo with Shift).

Another key for a built-in action goes in playbackShortcuts, splittingShortcuts, undoShortcuts or annotationShortcuts, one binding per action: <KeyboardShortcuts playback playbackShortcuts={{ playPause: { key: 'p' } }} /> plays on P and no longer on Space. An undoShortcuts binding that names neither Ctrl nor Cmd runs with either (and redo with Shift).

A key press runs one action, the first match in this order: your shortcuts, Stop while playing, an annotation key that applies, playback, splitting, undo.

Your own shortcuts go in additionalShortcuts. A shortcut runs only on its exact Ctrl, Cmd (metaKey) and Alt state: one that names none of them is the key alone, so { key: 's' } does not run on Cmd+S. Shift is the exception: left unset it matches with or without Shift, because Shift changes the key itself (?, {); shiftKey: true and shiftKey: false are exact. They are matched before every built-in key, the annotations keys included, so one on the same combination runs in its place: { key: ' ', action: myToggle } replaces the built-in Space. The annotations keys are listed in the Annotations guide.

Shortcuts during a drag​

While a clip is being moved or trimmed, or a track is being dragged to a new position, undo and redo do nothing: the keys, and useEditorControls().undo() / redo() called by the app. The drag does not change useUndoRedo().canUndo / canRedo: an Undo button stays enabled, and pressing it does nothing. Escape cancels the drag (a clip, a trim, a track or an annotation edge goes back to where it was) and does nothing else: playback goes on and the selected annotation stays selected. A second Escape stops playback as usual. A call to the arrangement's own undo() (useArrangement().undo(), or an Arrangement the app holds) is not stopped during a drag. An Escape that your own code calls preventDefault() on before the library's window listener runs fires no shortcut either (see the upgrade guide).

Shortcuts and focused controls​

A shortcut does not fire for a key that the focused control uses itself:

  • Text fields, sliders, checkboxes and every other <input>, <textarea>, <select> dropdowns and contenteditable elements keep every key.
  • Escape closes an open modal dialog (<dialog> opened with showModal(), or an aria-modal="true" container) and does not also stop playback, wherever focus is.
  • Space and Enter go to a focused <button> or <summary>, and Enter to a focused link.

While a text field, slider, checkbox or dropdown has focus, no shortcut fires at all, including undo and split.

How the control got focus makes no difference. A control you clicked still has focus, so it behaves as it would on any other page: after clicking Mute, Space toggles Mute again; after picking a value from a dropdown, Space opens the dropdown. Space is play/pause when focus is not on a control. Click an empty part of the page, or press Tab, to move focus off one.

The built-in Play, Pause and Stop buttons are disabled while they do not apply (Play while playing, for example), so focus leaves them when you click them and Space keeps working as play/pause.

Elements that are only given a role (<div role="button">) are not covered: a shortcut still fires while one has focus.

This applies to every shortcut handled through useKeyboardShortcuts / <KeyboardShortcuts>, including additionalShortcuts, and looks through shadow DOM.

Complete Example​

import {
WebAudioProvider,
Waveform,
PlayButton,
PauseButton,
StopButton,
RewindButton,
FastForwardButton,
SetLoopRegionButton,
LoopButton,
MasterVolumeControl,
AudioPosition,
TimeFormatSelect,
AutomaticScrollCheckbox,
useCreateArrangement,
} from '@waveform-playlist/browser';
import { audioTrack } from '@waveform-playlist/core';

function FullPlaybackExample() {
const arrangement = useCreateArrangement(() => [
audioTrack({ src: '/audio/track.mp3', name: 'Track' }),
]);

return (
<WebAudioProvider arrangement={arrangement}>
<div style={{ display: 'flex', gap: '1rem', padding: '1rem' }}>
<RewindButton />
<PlayButton />
<PauseButton />
<StopButton />
<FastForwardButton />
<SetLoopRegionButton />
<LoopButton />
<MasterVolumeControl />
<TimeFormatSelect />
<AudioPosition />
<AutomaticScrollCheckbox />
</div>
<Waveform />
</WebAudioProvider>
);
}

Next Steps​