Skip to main content

getCurrentTime

Get the current playback position of a sound in seconds. Use it for a time readout next to a seek bar.

getCurrentTime(id: string): number;

Parameters​

ParameterTypeDefaultDescription
idstringrequiredID of the sound, stream or overlapping instance.

Returns​

number: the position in the file in seconds, rounded to 4 decimals, whatever the playback rate. It is 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(() => {
timeLabel.textContent = `${soundHub.getCurrentTime('music').toFixed(1)}s`;
}, 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​

  • It returns getSoundState(id).currentTime. At playback rate 2, a sound ten seconds into its file reports 10, the same as a stream does. seek takes the same scale, and so does getDuration, so dividing the two gives the progress.
  • Before 6.4.0 a buffered sound divided this value by the playback rate. For the time it took to hear, read adjustedElapsedTime from getSoundState.
  • A paused sound keeps its position. A looping sound wraps back to 0 on every loop.
  • An unknown id logs an error, stores it for getLastError and dispatches an error event.

See also​