Skip to main content

registerSound

registerSound(id: string, url: string | string[]): void;
registerSounds(soundsToRegister: { id: string; url: string | string[] }[]): void;

Write down where a sound lives without fetching it. Use it for audio you know about but do not need at startup: the boss music, the sounds of level seven.

Added in 6.2.0.

const soundHub = new SoundHub();

soundHub.registerSounds([
{ id: 'boss-music', url: ['/audio/boss.opus', '/audio/boss.mp3'] },
{ id: 'victory', url: '/audio/victory.mp3' },
]);

soundHub.getLoadState('boss-music'); // 'unloaded'

Nothing is requested until you ask for it. When the moment arrives, loadSound needs no url, because the hub already has it:

await soundHub.loadSound('boss-music');
soundHub.play('boss-music', { loop: true });

A list of urls works the same way it does in loadSound: the browser picks the first format it can play, and only that file is fetched.

ParameterTypeDescription
idstringThe id you will address the sound by
urlstring | string[]Where it lives. A list lets the browser pick the format

Playing a sound that is registered but not loaded is an error, the same as playing an id the hub has never heard of. Load it first, or check getLoadState.

See also