How to Get Howler.js Instance Volume
This article explains how to quickly retrieve the current volume
level of a specific Howl instance in howler.js. You will
learn the exact method to use, how to interpret its return value, and
see a practical code example to implement in your project.
To determine the current volume level of a specific Howl
instance, you call the .volume() method on that instance
without passing any arguments. When invoked without parameters, this
method acts as a getter and returns a float value between
0.0 (completely silent) and 1.0 (full
volume).
Code Example
Here is how to initialize a sound and retrieve its volume level using JavaScript:
// Initialize the Howl instance with a specific volume
const sound = new Howl({
src: ['audio.mp3'],
volume: 0.755
});
// Retrieve the current volume level of this specific instance
const currentVolume = sound.volume();
console.log(currentVolume); // Outputs: 0.755Key Details to Remember
- Default Volume: If you do not define a volume
property when initializing the
Howlinstance, the default value returned by.volume()will be1.0. - Instance vs. Global Volume: The
sound.volume()method only retrieves the volume for that specific sound instance. If you need to check the global volume of all sounds currently managed by howler.js, use the global API:Howler.volume(). - Setting Volume: The
.volume()method is polymorphic. If you want to change the volume instead of reading it, pass a float value as an argument (e.g.,sound.volume(0.5)).