Skip to main content

getSoundVolume

Read the volume of one sound, for example to set a per-sound slider.

getSoundVolume(id: string): number;

Parameters​

ParameterTypeDefaultDescription
idstringrequiredID of the sound or stream.

Returns​

number: the sound's volume from 0 to 1. For an id that is not loaded you get 0.

Example​

import { SoundHub } from 'soundhub';

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

soundHub.setSoundVolume('music', 0.4);
soundHub.getSoundVolume('music'); // 0.4

soundHub.mute('music');
soundHub.getSoundVolume('music'); // still 0.4

Good to know​

  • A mute does not change the value: you get the volume the sound comes back to. The same goes for muteAllSounds and the master volume, which are not included.
  • During fadeIn or fadeOut the value follows the fade.
  • For a stream from loadStream it returns the stream's volume, which is 0 while the stream is muted.
  • For an id that is not loaded, the error is logged, stored for getLastError and dispatched as an error event.

See also​