Skip to main content

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​

ParameterTypeDefaultDescription
idstringrequiredID 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​

Try it

Read the playback position

Idle
getCurrentTime()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() or Math.round() for display.
  • When a sound reaches its end it becomes stopped and the percentage goes back to 0 (or to where the startTime option points), not 100.
  • Everything on getProgress applies here too, including the error that is logged for an unknown id.

See also​