javascriptaudio

How to set volume of audio object?


I know that i can create an audio object like this:

var audio = new Audio("test.wav");

And i know how i can play the audio:

audio.play();

I used the following for loop to output all functions from audio:

var myAudioObject = new Audio();

for (var key in myAudioObject)
{
   if (typeof myAudioObject[key] === "function")
   {
       console.log(key);
   }
}

But there is no setting for volume. Is it possible to change the volume in the audio object?


HINT

It was my fault. If i replace function in my for loop with number then i find volume.

var myAudioObject = new Audio();

for (var key in myAudioObject)
{
   if (typeof myAudioObject[key] === "number")
   {
       console.log(key);
   }
}


Solution

  • It's not a function, it's a property called volume.

    audio.volume = 0.2;
    

    The volume value is between 0 and 1:

    HTMLMediaElement volume MDN