Skip to main content

getDuration

Get how long a sound is, in seconds. Use it for a total-time label or to size a seek bar.

getDuration(id: string): number;

Parameters​

ParameterTypeDefaultDescription
idstringrequiredID of the sound, stream, sprite sound or overlapping instance.

Returns​

number: the length of the audio in seconds. 0 for an id that is not loaded, and for a stream whose length is not known.

Example​

import { SoundHub } from 'soundhub';

const soundHub = new SoundHub();
await soundHub.loadSound('music', '/audio/theme.mp3');

const duration = soundHub.getDuration('music');
totalLabel.textContent = `${Math.floor(duration / 60)}:${String(Math.floor(duration % 60)).padStart(2, '0')}`;

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​

  • This is the length of the file. It does not change with the playback rate, and the duration play option does not shorten it. getCurrentTime and seek use the same scale.
  • A sprite sound such as ui_jump reports the length of its range.
  • A live stream, or a stream whose metadata has not arrived, reports 0 instead of Infinity or NaN.
  • For an id that is not loaded, the error is logged, stored for getLastError and dispatched as an error event.

See also​