Skip to main content

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​

ParameterTypeDefaultDescription
idstringrequiredID of the sound or stream.
volumenumberrequiredVolume from 0 (silent) to 1 (full). Values outside that range are clamped.
skipDispatchEventbooleanfalseWhen 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​

Try it

Change the volume of one sound

Idle
60%
01

Drag 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 at 1 plays at half.
  • Dispatches SoundEventsEnum.VOLUME_CHANGED ('volume_changed'). Works on streams from loadStream too. For an id that is not loaded, the error is logged, stored for getLastError and dispatched as an error event.

See also​