getProgress
Get how far along a sound is, as a ratio from 0 to 1. It is the number a seek bar or a progress ring wants.
getProgress(id: string): number;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
id | string | required | ID of the sound, stream or overlapping instance. |
Returns
number: the position divided by the length, from 0 to 1. 0 for a stopped sound, for an id that is not loaded, and when the length is unknown.
Example
import { SoundHub } from 'soundhub';
const soundHub = new SoundHub();
await soundHub.loadSound('music', '/audio/theme.mp3');
soundHub.play('music');
function render() {
seekBar.value = soundHub.getProgress('music');
requestAnimationFrame(render);
}
render();
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 ratio is right at any playback rate, because time and length are both taken from
getSoundStateand adjusted the same way. - When a sound reaches its end it becomes stopped and the progress goes back to
0(or to where thestartTimeoption points), not1. Listen for theendedevent if you need to know it finished. - A looping sound wraps back to 0 on every loop. A paused sound keeps its value.
- The value is not rounded, unlike
getSoundState(id).progress. - An unknown id logs an error, stores it for
getLastErrorand dispatches anerrorevent.
See also
getProgressPercentage: the same value from 0 to 100.startProgressTracking: getprogressevents instead of polling.seek: jump to a position.