Skip to main content

getLoadState

getLoadState(id: string): SoundLoadState;

Where a sound is in the loading process. isSoundLoaded(id) stays the quick yes or no; this is the answer when you want to show a spinner or a retry button.

Added in 6.2.0.

type SoundLoadState = 'unloaded' | 'loading' | 'loaded' | 'error';
StateMeaning
unloadedThe hub has never heard of this id, or it was registered with registerSound and not fetched yet
loadingA fetch is in flight
loadedThe audio is decoded and ready to play
errorThe fetch or the decode failed. The url is still on file, so you can try again
soundHub.registerSound('boss-music', '/audio/boss.mp3');
soundHub.getLoadState('boss-music'); // 'unloaded'

const loading = soundHub.loadSound('boss-music');
soundHub.getLoadState('boss-music'); // 'loading'

await loading;
soundHub.getLoadState('boss-music'); // 'loaded'

A loading event goes on the event bus when a fetch starts, and loaded when it finishes, so a progress bar does not have to poll:

soundHub.addEventListener(SoundEventsEnum.LOADING, (event) => {
spinner.show(event.soundId);
});

soundHub.addEventListener(SoundEventsEnum.LOADED, (event) => {
spinner.hide(event.soundId);
});

A stream loaded with loadStream reports loaded as soon as the browser has its metadata.

See also