getProgressPercentage
Get how far along a sound is, as a percentage from 0 to 100. Handy for a CSS width or a text label.
getProgressPercentage(id: string): number;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
id | string | required | ID of the sound, stream or overlapping instance. |
Returns
number: getProgress times 100. 0 for a stopped sound and for an id that is not loaded.
Example
import { SoundHub } from 'soundhub';
const soundHub = new SoundHub();
await soundHub.loadSound('music', '/audio/theme.mp3');
soundHub.play('music');
setInterval(() => {
const pct = soundHub.getProgressPercentage('music');
progressFill.style.width = `${pct}%`;
progressLabel.textContent = `${pct.toFixed(1)}%`;
}, 250);
Try it
Read the playback position
IdlegetCurrentTime()0.0 s
getDuration()0.0 s
getProgress()0.000
getProgressPercentage()0%
0:00 of 0:00. getProgress is a ratio from 0 to 1, getProgressPercentage the same value from 0 to 100.
Code
Your clicks show up here as soundhub calls
Good to know
- The value is not rounded. Use
toFixed()orMath.round()for display. - When a sound reaches its end it becomes stopped and the percentage goes back to
0(or to where thestartTimeoption points), not100. - Everything on
getProgressapplies here too, including the error that is logged for an unknown id.
See also
getProgress: the same value as a ratio from 0 to 1.getCurrentTime: the position in seconds.startProgressTracking: getprogressevents instead of polling.