Skip to main content

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​

ParameterTypeDefaultDescription
idstringrequiredID 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:1 and sprite sounds such as ui_jump are entries of their own, so hasSound returns true for them while they exist.
  • It never logs an error, unlike getters such as getBuffer that validate the id.

See also​