Skip to main content

MediaElementProvider

A simplified playlist provider for single-track playback using HTMLAudioElement. Ideal for language learning apps, podcast players, and single-track audio viewers.

Key Differences from WebAudioProvider​

FeatureMediaElementProviderWebAudioProvider
InstancesMultiple per pageSingle instance only (shared Tone.js Transport)
TracksSingle track (track prop)Multiple tracks (an arrangement, the arrangement prop)
Audio engineHTMLAudioElementTone.js / Web Audio API
Waveform dataPre-computed (WaveformDataObject)Decoded from AudioBuffer
Playback ratePitch-preserving (0.5x - 2.0x)Not built-in
EditingView onlyDrag, trim, split
EffectsVia Tone.js bridge (see guide)20 built-in Tone.js effects

Import​

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

Basic Usage​

import { MediaElementProvider, MediaElementWaveform } from '@waveform-playlist/browser';

<MediaElementProvider
track={{
source: '/audio/episode.mp3',
waveformData: waveformDataJson,
name: 'Episode 1',
}}
samplesPerPixel={1024}
>
<MediaElementWaveform />
</MediaElementProvider>

The options that only affect how the view draws (row height, bar style, playhead width) are not provider props. Set them with <WaveformViewOptions>, which may sit above several providers:

import {
MediaElementProvider,
MediaElementWaveform,
WaveformViewOptions,
} from '@waveform-playlist/browser';

<WaveformViewOptions waveHeight={100} barWidth={2} barGap={1}>
<MediaElementProvider track={episodeOne}>
<MediaElementWaveform />
</MediaElementProvider>
<MediaElementProvider track={episodeTwo}>
<MediaElementWaveform />
</MediaElementProvider>
</WaveformViewOptions>

Props​

Required Props​

track​

Type: MediaElementTrackConfig

Single track configuration object.

interface MediaElementTrackConfig {
/** Audio source URL or Blob URL */
source: string;
/** Pre-computed waveform data (required for visualization) */
waveformData: WaveformDataObject;
/** Track name for display */
name?: string;
/** Fade in configuration (requires audioContext on provider) */
fadeIn?: FadeConfig;
/** Fade out configuration (requires audioContext on provider) */
fadeOut?: FadeConfig;
}

// FadeConfig is from @waveform-playlist/core
interface FadeConfig {
/** Duration of the fade in seconds */
duration: number;
/** Type of fade curve (default: 'linear') */
type?: 'linear' | 'logarithmic' | 'exponential' | 'sCurve';
}

Display Props​

samplesPerPixel​

Type: number Default: 1024

Zoom level for waveform rendering. The provider follows it: a new value redraws the waveform at it, and the player is not rebuilt. There is no zoom control under this provider (canZoomIn and canZoomOut are always false), so this prop is the only place the level is set. A value that is not a finite number above 0 (0, a negative number, NaN, Infinity, text) is drawn as 1024, with one console warning.

On a change the view keeps one point in place, as far as the scroll range allows: the time under a mouse or pen hovering over the timeline (the tracks or the ruler); without one, a selected range that is on screen and does not fill the view (its centre, or the centre of its visible part); else the cursor, when it is on screen; else the centre of the view. A value finer than the waveform data (below its scale, the samples per pixel the .dat file was made at) is drawn at the data's own resolution, and useZoom().samplesPerPixel reports that value.

waveHeight, barWidth, barGap, roundedBars and progressBarWidth are props of <WaveformViewOptions>, not of the provider. waveHeight is the height of one channel and is 80 px by default (it was 100 while it was a prop of this provider); the row is that times the number of channels, plus the clip header when the view has one. A leftover one on the provider is a compile error in TypeScript and is ignored in plain JavaScript (the defaults are drawn). See the upgrade guide.

The time ruler is not a provider prop. It is a part of the view: <MediaElementWaveform /> draws it, and a composition has it when it renders <WaveformTimescale /> inside <MediaElementWaveformRoot>. There is no controls column for this player. See Composing the View.

Web Audio​

audioContext​

Type: AudioContext

When provided, audio routes through a Web Audio graph enabling fades and effects. Required for fadeIn/fadeOut on the track config and for bridging into Tone.js effects. Each provider instance should use its own AudioContext — createMediaElementSource() is called once per audio element.

Playback​

playbackRate​

Type: number Default: 1

The playback rate. The bundled engine is created at this speed as given; the provider does not clamp the prop (its setPlaybackRate clamps to 0.5–2). The user can change the speed with setPlaybackRate from usePlaybackRate() (the PlaybackRateSelect control); a later change of this prop replaces their choice, for the audio and for the value usePlaybackRate() returns, until they choose again. A player that replaces another plays at the current speed, with two exceptions: a child that sets a speed in a useLayoutEffect in the same commit as a change of this prop (the new player gets the prop's speed; use useEffect), and a setPlaybackRate call made while the provider is inside a hidden <Activity> after this prop changed while hidden (on reveal the player gets the prop's speed). A player from a custom createPlayout is not created with the speed: the provider calls its setPlaybackRate with the current speed when it arrives. Changing the prop recreates the player.

preservesPitch​

Type: boolean Default: true

Whether to preserve pitch when changing playback rate. When true, the browser's built-in time-stretching keeps pitch constant while speed changes. Set to false when using an external pitch processor (e.g., SoundTouch AudioWorklet) that handles pitch compensation itself.

automaticScroll​

Type: boolean Default: false

Auto-scroll to keep the playhead centered during playback. This is the initial value: afterwards the provider owns the setting, which usePlaybackControls().setAutomaticScroll() (or the AutomaticScrollCheckbox) changes, and a later change of the prop is not followed. While it is on, a drag on the waveform during playback does not select a range: the press and its release seek to where the press landed.

Theming​

theme​

Type: Partial<WaveformTheme>

Theme keys to change. The provider merges the object over defaultTheme ({ ...defaultTheme, ...theme }), so a key left out keeps its defaultTheme value; without the prop the theme is defaultTheme. It is a provider prop and not a view option: the transport controls, the checkboxes and the annotation list are drawn outside a view and read it too. See Theming Guide.

Annotations​

annotationStore​

Type: AnnotationStore (from @waveform-playlist/core)

The owner of the annotations. Make it with useCreateAnnotationStore and change the annotations with its methods, set(list), update(fn) and rename(from, to). The provider publishes what the store holds (useAnnotations()) and writes nothing back; the edge drag, the boundary keys and the list's text, id and action edits write to the store. The same prop on both providers, and the same store works under either.

Without it there are no annotations: useAnnotations().annotations is [], and the provider makes no store of its own. usePlaybackControls().annotationStore is this prop, or undefined.

const annotationStore = useCreateAnnotationStore(() => initialAnnotations);

<MediaElementProvider track={track} annotationStore={annotationStore}>

To save the annotations, subscribe to the store (annotationStore.subscribe(…)) or read it with useStoredAnnotations(annotationStore). See The Annotation Store for beforeChange and what the store refuses.

annotationList​

Type: AnnotationListProp: { editable?: boolean; linkEndpoints?: boolean; isContinuousPlay?: boolean }

The starting values of the three annotation options. The same prop on both providers. The annotations are not here: they are annotationStore's.

  • editable: annotations can be edited (text, ids, edges). Default false.
  • linkEndpoints: a dragged edge takes the edge it touches along. Default false.
  • isContinuousPlay: a play of an annotation (a click on its box or row, playAnnotation) runs on past the annotation's end. Default false. A play started any other way is never cut at an annotation's end.

The three options are provider state that follows the prop: each starts from its field, and when the value of a field changes, the option takes the new value. A field left out is false. Between two changes of the prop, a setter of usePlaybackControls() (setAnnotationsEditable, setLinkEndpoints, setContinuousPlay) or one of the checkboxes changes the option; the prop is not told. Read the options with useAnnotationOptions().

The action buttons of the annotation list are not set here: pass them to <AnnotationList actions>.

Callbacks​

onReady​

Type: () => void

Called when the media element has loaded its metadata: it knows its length and can be played and seeked. Called each time a player's media has loaded: once per player (the player is created again when the track or one of the player options changes), and again if your code calls playoutRef.current.setSource(). Not called for a source that cannot be loaded; see onError. Some browsers load metadata only when playback is asked for, so do not make Play wait for it. Pass a stable function: a new identity creates the player again.

onError​

Type: (error: Error) => void

Called when the player could not be created (a missing optional peer, a throwing createPlayout, an error from the engine's addTrack) and when the media element reports an error (a source that cannot be loaded or decoded, or a network failure during playback, which also ends playback). The provider does not put the source URL in the message. The provider also logs a warning. Pass a stable function.

Context Hooks​

MediaElementProvider publishes what it shares with WebAudioProvider through the same hooks, so a component written against them works under either provider. The interfaces are in the hooks reference.

HookReturns under this provider
useIsPlaying()isPlaying
useCurrentTime()currentTime: where the cursor rests. It does not advance during playback
useSelection()selectionStart, selectionEnd
useActiveAnnotation()activeAnnotationId (the selection; playback and Stop do not change it), playingAnnotationId (the annotation the playhead is in while playing; read-only). New audio sets both to null; the selection is also null once its annotation leaves the store
useAnnotations()annotations
useAnnotationOptions()editable, linkEndpoints, continuousPlay
usePlaybackControls()play, pause, stop, seekTo, setSelection, setTimeFormat, setAutomaticScroll, setScrollContainer, scrollContainerRef, setContinuousPlay, setAnnotationsEditable, setLinkEndpoints, annotationPreview, annotationStore, setActiveAnnotationId, playAnnotation
usePlaybackRefs()currentTimeRef, visualTimeRef (the same ref as currentTimeRef here: a media element reports the audible position), registerFrameCallback, unregisterFrameCallback
useAudioSettings()sampleRate; mono and indefinitePlayback are always false. The view options are not here: inside a root, useWaveformViewInfo() from @waveform-playlist/ui-components has the resolved waveHeight, barWidth, barGap, roundedBars and progressBarWidth
useDisplaySettings()timeFormat (default 'hh:mm:ss.uuu'), isAutomaticScroll
useZoom()samplesPerPixel; canZoomIn and canZoomOut are always false

Two hooks exist under this provider only:

usePlaybackRate​

The playback speed and its setter.

interface PlaybackRateContextValue {
/** The speed `setPlaybackRate` set last (0.5 to 2), or the provider's `playbackRate`
* prop as given (the provider does not clamp it). */
playbackRate: number;
/** Clamped to 0.5–2; NaN is ignored. Stable identity. */
setPlaybackRate: (rate: number) => void;
}

useMediaElementTrack​

The one track of the player.

interface MediaElementTrackContextValue {
/** The length of the track's audio, from its waveform data. Never 0 for a real track. */
duration: number;
/**
* True once the media element has loaded its metadata, false again while a
* replaced player loads. Use this, not `duration`, as "the player can be used".
*/
isReady: boolean;
/** One track with one clip (`[]` when the peak data could not be read). The clip carries the track's fadeIn / fadeOut. */
peaksDataArray: TrackClipPeaks[];
/** The player. `current` is null until it has been created. */
playoutRef: React.RefObject<MediaElementPlayout | null>;
}

Hooks that throw under this provider​

useEditorControls(), useEngineRefs(), useTimeline(), useMixer(), useTrackReorderPreview(), useLoop(), useSelectedTrackId() and useUndoRedo() need WebAudioProvider. Under this provider they throw, also when it is nested inside a WebAudioProvider: every hook reads the nearest playlist provider. The message reads, for example, "useLoop needs WebAudioProvider, and the nearest playlist provider here is MediaElementProvider, which does not provide it".

Controls​

The library's PlayButton, PauseButton, StopButton, RewindButton, FastForwardButton, SkipBackwardButton, SkipForwardButton, ClearAllButton, AudioPosition, TimeFormatSelect, SelectionTimeInputs, AutomaticScrollCheckbox and AnnotationList (which draws nothing without <AnnotationProvider>) work under this provider, and so do ContinuousPlayCheckbox, LinkEndpointsCheckbox, EditableCheckbox and DownloadAnnotationsButton of @waveform-playlist/annotations. PlaybackRateSelect works under this provider only. KeyboardShortcuts, the loop and zoom buttons, MasterVolumeControl and ExportWavButton do not: there is no loop and no zoom here, and the library installs no keyboard shortcuts (usePlaybackShortcuts() throws here too; useKeyboardShortcuts() and useAnnotationKeyboardControls() can be called by your own component). See Components.

playoutRef.current.outputNode returns the native GainNode output — use it to bridge into Tone.js effect chains (see guide).

TypeScript​

Full type definition:

interface MediaElementProviderProps {
// Required
track: MediaElementTrackConfig;
children: React.ReactNode;

// Display
samplesPerPixel?: number;

// Web Audio
audioContext?: AudioContext;

// Playback
playbackRate?: number;
preservesPitch?: boolean;
automaticScroll?: boolean;

// Theming
theme?: Partial<WaveformTheme>;

// Annotations
annotationStore?: AnnotationStore;
annotationList?: AnnotationListProp; // { editable?, linkEndpoints?, isContinuousPlay? }

// Callbacks
onReady?: () => void;
onError?: (error: Error) => void;

// Custom engine
/** Supply a custom MediaElementPlayout — skips dynamic import of @waveform-playlist/media-element-playout.
* It must implement on() and emit 'loadedmetadata'. */
createPlayout?: () => MediaElementPlayout;
}

See Also​