setSoundVolume
Set the volume of one sound. The change is immediate, also while the sound plays. Use setGlobalVolume to scale all sounds at once.
setSoundVolume(id: string, volume: number, skipDispatchEvent?: boolean): void;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
id | string | required | ID of the sound or stream. |
volume | number | required | Volume from 0 (silent) to 1 (full). Values outside that range are clamped. |
skipDispatchEvent | boolean | false | When true, no volume_changed event is dispatched. |
Returns
Nothing.
Example
import { SoundHub } from 'soundhub';
const soundHub = new SoundHub();
await soundHub.loadSound('music', '/audio/theme.mp3');
soundHub.play('music', { loop: true });
soundHub.setSoundVolume('music', 0.5); // half
soundHub.setSoundVolume('music', 0); // silent, still playing
Try it
Change the volume of one sound
IdleDrag the slider while the sound plays. The change is immediate.
Code
Your clicks show up here as soundhub calls
Good to know
- A fade that is running on the sound is cancelled.
- The sound is no longer muted afterwards: setting a volume ends a
mute. - The value is stored in the sound's play options, so the next
play()uses it. - The master volume still applies on top. With the master at
0.5, a sound at1plays at half. - Dispatches
SoundEventsEnum.VOLUME_CHANGED('volume_changed'). Works on streams fromloadStreamtoo. For an id that is not loaded, the error is logged, stored forgetLastErrorand dispatched as anerrorevent.
See also
getSoundVolume: read the volume of a sound.fadeIn,fadeOut: change the volume over time.setGlobalVolume: the master volume.