getLoadState
Get where a sound is in the loading process. isSoundLoaded is the quick yes or no; this is the answer when you want to show a spinner or a retry button.
getLoadState(id: string): SoundLoadState;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
id | string | required | ID of the sound or stream. |
Returns
SoundLoadState, one of 'unloaded' | 'loading' | 'loaded' | 'error':
| State | Meaning |
|---|---|
unloaded | The hub has never heard of this id, it was registered with registerSound and not fetched yet, or it was unloaded. |
loading | A fetch is in flight. |
loaded | The audio is decoded and ready to play, or the id is a stream. |
error | The fetch or the decode failed. The url is still on file, so you can try again. |
An unknown id gives 'unloaded'.
Example
import { SoundHub, SoundEventsEnum } from 'soundhub';
const soundHub = new SoundHub();
soundHub.registerSound('music', '/audio/theme.mp3');
soundHub.getLoadState('music'); // 'unloaded'
const loading = soundHub.loadSound('music');
soundHub.getLoadState('music'); // 'loading'
await loading;
soundHub.getLoadState('music'); // 'loaded'
A loading event is dispatched when a fetch starts and loaded when it finishes, so a spinner does not have to poll:
soundHub.addEventListener(SoundEventsEnum.LOADING, (event) => {
spinner.show(event.soundId);
});
soundHub.addEventListener(SoundEventsEnum.LOADED, (event) => {
spinner.hide(event.soundId);
});
Good to know
- A stream loaded with
loadStreamreportsloadedonce the browser has its metadata. Before that it reportsunloaded, because streams have noloadingstate. unloadSound,removeSoundandresetwithunloadSounds: trueput a sound back tounloaded. Before 6.4.0 the reset left it atloaded.- Retrying after
erroris a plainloadSound(id): the url list is kept. - Added in 6.2.0.
See also
isSoundLoaded: the plain yes or no.registerSound: record a url without fetching it.soundEvents: theloading,loadedanderrorevents.