SoundProgressStateInfo
The progressInfo object on a progress event. While a sound plays with progress tracking on, the hub dispatches SoundEventsEnum.PROGRESS every 50 ms by default, and this object tells you where the sound is.
export interface SoundProgressStateInfo {
soundId: string;
currentTime: number;
duration: number;
rawDuration: number;
progress: number; // 0-1
}
| Field | Type | Description |
|---|---|---|
soundId | string | The sound the event is for. For an overlapping instance this is the instance id, like 'click:2'. |
currentTime | number | Position in seconds, adjusted for the playback rate. |
duration | number | Length in seconds, adjusted for the playback rate. At rate 2 a 60 second file reports 30. 0 when unknown. |
rawDuration | number | Length of the file itself in seconds, whatever the rate. 0 when unknown. |
progress | number | How far along the sound is, from 0 to 1. |
SoundProgressStateInfo is exported from soundhub as a type. There is no percentage or remaining time field: multiply progress by 100, or subtract currentTime from duration.
Example
import { SoundHub, SoundEventsEnum } from 'soundhub';
const soundHub = new SoundHub();
await soundHub.loadSound('music', '/audio/theme.mp3');
soundHub.addEventListener(
SoundEventsEnum.PROGRESS,
(event) => {
const info = event.progressInfo;
if (!info) return;
bar.style.width = `${info.progress * 100}%`;
label.textContent = `${formatTime(info.currentTime)} / ${formatTime(info.duration)}`;
},
{ soundId: 'music' },
);
soundHub.play('music');
function formatTime(seconds: number): string {
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins}:${secs.toString().padStart(2, '0')}`;
}
Try it
Start the sound and watch the progress values arrive as progress events fire.
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
- Progress tracking is on by default:
trackProgressinSoundHubConfigistrue, and sounds and streams take it from there. Turn it off per sound withplay(id, { trackProgress: false }), or switch it by hand withstartProgressTrackingandstopProgressTracking. - The same values are also on the event itself:
event.currentTime,event.durationandevent.progress, plus the fullSoundStateInfoinevent.state. - Tracking stops by itself when the sound is no longer playing, so a paused sound sends no progress events.
setProgressUpdateIntervalchanges how often the events fire, for every sound.- For a stream,
rawDurationanddurationare both the length the audio element reports.
See also
getProgress: read the progress ratio without an event.getCurrentTime: the current position in seconds.getDuration: the length of a sound.setProgressUpdateInterval: how often progress events fire.soundEvents: every event the hub dispatches.