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
| Parameter | Type | Default | Description |
|---|---|---|---|
id | string | required | ID 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
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
- It returns
getSoundState(id).currentTime. At playback rate 2, a sound ten seconds into its file reports10, the same as a stream does.seektakes the same scale, and so doesgetDuration, 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
adjustedElapsedTimefromgetSoundState. - 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
getLastErrorand dispatches anerrorevent.
See also
getDuration: the length of the sound.getProgress: the position as a ratio.seek: jump to a position.getSoundState: time, duration and state in one object.