Video volume Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Video

Description

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

The <video> element is new in HTML5.

Set the volume property with the following Values

Value Description
number Sets the audio volume of the video. 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 video

Default Value is 1.0

The following code shows how to Set video volume to 20%:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<video id="myVideo" width="320" height="240" controls>
  <source src="your.mp4" type="video/mp4">
  <source src="your.ogg" type="video/ogg">
  Your browser does not support the video tag.
</video>// w  w w  .j a 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("myVideo");

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

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

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

</body>
</html>

Related Tutorials