hasSound
Check whether an id is loaded and ready to use. Reach for it before calling methods that log an error for an unknown id.
hasSound(id: string): boolean;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
id | string | required | ID of the sound or stream to check. |
Returns
boolean: true for a loaded sound or stream. false for an unknown id, and for one that is only registered or still loading.
Example
import { SoundHub } from 'soundhub';
const soundHub = new SoundHub();
await soundHub.loadSound('click', '/audio/click.mp3');
soundHub.hasSound('click'); // true
soundHub.hasSound('rain'); // false
if (soundHub.hasSound('click')) {
soundHub.play('click');
}
Good to know
- It gives the same answer as
isSoundLoaded. - Overlapping instances such as
click:1and sprite sounds such asui_jumpare entries of their own, sohasSoundreturnstruefor them while they exist. - It never logs an error, unlike getters such as
getBufferthat validate the id.
See also
isSoundLoaded: the same check under another name.getLoadState: tells loading and error apart from unloaded.getSoundIds: every id in the hub.