Audio volume Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Audio

Description

The volume property sets or gets the audio volume of an audio, from 0.0 (silent) to 1.0 (loudest).

Set the volume property with the following Values

Value Description
number Sets the audio volume of the audio. Must be a number between 0.0 to 1.0

Example values:

  • 1.0 is highest volume 100%. This is default
  • 0.5 is half volume (50%)
  • 0.0 is silent (same as mute)

Return Value

A Number, representing the audio volume of the audio

The following code shows how to set audio volume to 20%:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<audio id="myAudio" controls>
  <source src="your.ogg" type="audio/ogg">
  <source src="your.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>//  w  w w  . ja v  a  2 s .c  o m

<button onclick="getVolume()" type="button">What is the volume?</button>
<button onclick="setHalfVolume()" type="button">Set volume to 0.2</button>
<button onclick="setFullVolume()" type="button">Set volume to 1.0</button>

<script>
var x = document.getElementById("myAudio");

function getVolume() {
    console.log(x.volume);
}

function setHalfVolume() {
    x.volume = 0.2;
}

function setFullVolume() {
    x.volume = 1.0;
}
</script>

</body>
</html>

Related Tutorials