Skip to main content

getSound

Get the Sound object the hub keeps for an id: the decoded buffer, the audio nodes, the state and the play options. Reach for it when you need to inspect something no dedicated getter exposes.

getSound(id: string): Sound | undefined;

Parameters​

ParameterTypeDefaultDescription
idstringrequiredID of a loaded sound, an overlapping instance such as click:1, or a sprite sound such as ui_jump.

Returns​

Sound | undefined: the live sound object, or undefined for an unknown id and for a stream.

Example​

import { SoundHub } from 'soundhub';

const soundHub = new SoundHub();
await soundHub.loadSound('music', '/audio/theme.mp3');

const sound = soundHub.getSound('music');
if (sound) {
console.log(sound.state); // 'stopped'
console.log(sound.buffer.duration); // length in seconds
console.log(sound.playOptions?.loop); // false
}

Good to know​

  • This is the object the hub works with, not a copy. Changing its fields goes around the hub and can put it out of step. Use the methods, such as updateSoundOptions or setSoundVolume, to change things.
  • It never logs an error for an unknown id.
  • Streams are kept separately and have no Sound object. Use getStreamElement or getSoundState for them.

See also​