Black
Black

Reputation: 20212

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);
   }
}

Upvotes: 72

Views: 98784

Answers (2)

Alexander Kravets
Alexander Kravets

Reputation: 4385

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

audio.volume = 0.2;

The volume value is between 0 and 1:

  • 1.0 means highest volume (100%. This is default)
  • 0.0 means silent (like as mute)

HTMLMediaElement volume MDN

Upvotes: 118

Solomon Kruse
Solomon Kruse

Reputation: 49

I use:

let music = new Audio({
    loop: true,
    volume: 1,
    src: ['/yourSounds/music.mp3']
})

it is easer to type

Upvotes: 0

Related Questions