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
| Parameter | Type | Default | Description |
|---|---|---|---|
id | string | required | ID 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
updateSoundOptionsorsetSoundVolume, to change things. - It never logs an error for an unknown id.
- Streams are kept separately and have no
Soundobject. UsegetStreamElementorgetSoundStatefor them.
See also
getSoundState: a computed snapshot for a player UI.getBuffer: only the decoded audio.getGainNode: the node that carries a sound's volume.