Playing and seeking
People seek with a click, a drag or the keys; your code seeks, binds another sound and changes the drawing through a few methods. Playing itself stays with the media element.
How people seek
- Click or tap: jumps there.
- Drag (touch included): scrubs, and seeks once when released. A cancelled drag changes nothing.
-
Keys, with the wave focused: the
arrows step by
stepSeconds(5), Page Up and Page Down bylargeStepRatio(a tenth), Home and End go to the ends. -
A vertical swipe still scrolls the page (
touch-action: pan-y).
Seeking from code
frame.seekTo(0.5); // the middle of the sound; the media moves too
frame.on('seek', ({ ratio, time, source }) => {
console.log(`Moved to ${time} s by ${source}`);
});
Positions are ratios of the sound, from 0 to 1, so they
never depend on the screen. source says who
moved it: the pointer, the keyboard or the API.
Changing the sound
| Method | What it does |
|---|---|
bind(media) |
Follow another audio or video element,
or none (null).
|
setPeaks(source) |
New peaks: a list,
{ peaks, duration }, a
loader or null.
|
setDuration(seconds) |
The length to use until the media knows its own. |
setProgress(ratio) |
The position shown while no media is bound. |
Changing the look
| Method | What it does |
|---|---|
setWave(wave) |
Another drawing (a name) or some of its details. |
setBar(bar) |
Part of the bar style (width, gap, radius…). |
setColors(colors) |
Some of the colors. |
setLabel(label) |
The accessible name. |
refresh() |
Reads the colors again after your own theme switch (the system's dark mode is followed on its own). |
Reading the state
progress (0 to 1),
duration (seconds, or null),
peaksDuration (the length the peaks came
with) and view (the part in view) are
read-only properties.
Next steps
- Events: listen to what happens.
- Frame methods and events: the full reference.