Skip to main content

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​

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

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 ratio is right at any playback rate, because time and length are both taken from getSoundState and adjusted the same way.
  • When a sound reaches its end it becomes stopped and the progress goes back to 0 (or to where the startTime option points), not 1. Listen for the ended event 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 getLastError and dispatches an error event.

See also​