Javascript DOM onvolumechange Event set volume

Introduction

Using the volume property to set the volume:

document.getElementById("myVideo").volume = 0.2;

Change the volume by either clicking the buttons or by adjusting the volume in the bottom right corner.

View in separate window

<!DOCTYPE html>
<html>
<body>

<video id="myVideo" width="100" height="100" controls>
  <source src="video.mp4" type="video/mp4">
  <source src="video.ogg" type="video/ogg">
  Your browser does not support the video tag.
</video>/*from w w  w  .j a va2s .  c  o m*/
<button onclick="setHalfVolume()" type="button">Set volume to 0.2</button>
<button onclick="setFullVolume()" type="button">Set volume to 1.0</button>

<p id="demo"></p>

<script>
// Get the video element with id="myVideo"
var x = document.getElementById("myVideo");

// Attach a "volumechange" event to the video
x.addEventListener("volumechange", getVolume);

function getVolume() {
  document.getElementById("demo").innerHTML = "The audio volume is: " + x.volume;
}
// Set the audio volume to 0.2
function setHalfVolume() {
  x.volume = 0.2;
}

// Set the audio volume to 1.0 (the highest)
function setFullVolume() {
  x.volume = 1.0;
}
</script>

</body>
</html>



PreviousNext

Related