startProgressTracking
Start dispatching progress events for a playing sound. You rarely need to call it: with the default trackProgress: true in the config, every play() starts tracking by itself. Call it when you turned tracking off and want it for one sound.
startProgressTracking(id: string): void;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
id | string | required | ID of a playing sound, stream or overlapping instance. |
Returns
Nothing.
Example
import { SoundHub, SoundEventsEnum } from 'soundhub';
const soundHub = new SoundHub({ trackProgress: false });
await soundHub.loadSound('music', '/audio/theme.mp3');
soundHub.addEventListener(SoundEventsEnum.PROGRESS, (event) => {
seekBar.value = event.progress ?? 0;
timeLabel.textContent = `${event.currentTime?.toFixed(1)}s`;
}, { soundId: 'music' });
soundHub.play('music');
soundHub.startProgressTracking('music');
Good to know
- Each event is
SoundEventsEnum.PROGRESS('progress') withcurrentTime,duration,progress, aprogressInfoobject (seesoundProgressStateInfo) and the fullgetSoundStatesnapshot asstate. - Updates run on
requestAnimationFrame, at most once per interval (50 ms by default, seesetProgressUpdateInterval). Browsers pause animation frames in a background tab, so no events arrive there. - Tracking stops by itself when the sound pauses, stops or ends, and on the first update if the sound is not playing. Call it after
play(), not before. - Calling it again for the same id replaces the running tracker, so there are never two.
- The tracker also enforces the
durationplay option: when the range is used up it ends, loops or pauses the sound (withpauseAtDurationReached).
See also
stopProgressTracking: stop the events for one sound.setProgressUpdateInterval: change how often they fire.addEventListener: listen for theprogressevents.getProgress: read the progress on demand instead.