Horje
HTML onvolumechange Attribute

Definition and Usage

The onvolumechange attribute defines a script to run each time the volume of a video/audio has been changed.

This event is invoked by:

  • Increasing or decreasing the volume
  • Muting or unmuting the media player

Tip: Use the volume property of the Audio/Video Object to set or return the audio volume of an audio/video.


Applies to

The onvolumechange attribute is part of the Event Attributes, and can be used on the following elements:

Elements Event
<audio> volumechange
<video> volumechange

Browser Support

The onvolumechange attribute has the following browser support for each element:


How to Run "myFunction" when the volume of the audio changes - HTML onvolumechange Attribute

Audio Example. Change the volume of the audio. This example demonstrates how to use the "onvolumenchange" attribute on an AUDIO element.
index.html
Example: HTML
<!DOCTYPE html> 
<html> 
<body> 

<p id="demo">Change the volume of the audio:</p>

<audio id="myAudio" controls onvolumechange="myFunction()">
  <source src="https://file-examples.com/wp-content/storage/2017/11/file_example_MP3_2MG.mp3" type="audio/ogg">
  <source src="https://file-examples.com/wp-content/storage/2017/11/file_example_MP3_2MG.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>

<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "You changed the volume";
}
</script> 

<p>This example demonstrates how to use the "onvolumenchange" attribute on an AUDIO element.</p>

</body> 
</html>

Output should be:

How to Run "myFunction" when the volume of the audio changes - HTML onvolumechange Attribute

How to Run "myFunction" when the volume of the video changes - HTML onvolumechange Attribute

Video Example. Change the volume of the video. This example demonstrates how to use the "onvolumechange" attribute on a VIDEO element.
index.html
Example: HTML
<!DOCTYPE html> 
<html> 
<body>

<p id="demo">Change the volume of the video:</p>

<video id="myVideo" width="320" height="176" controls onvolumechange="myFunction()">
  <source src="https://www.sample-videos.com/video321/mp4/240/big_buck_bunny_240p_30mb.mp4" type="video/mp4">
  <source src="https://www.sample-videos.com/video321/mp4/240/big_buck_bunny_240p_30mb.mp4" type="video/ogg">
  Your browser does not support HTML5 video.
</video>

<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "You changed the volume";
}
</script> 

<p>This example demonstrates how to use the "onvolumechange" attribute on a VIDEO element.</p>

<p>Video courtesy of <a href="https://www.bigbuckbunny.org/" target="_blank">Big Buck Bunny</a>.</p>

</body> 
</html>

Output should be:

How to Run "myFunction" when the volume of the video changes - HTML onvolumechange Attribute




html onvolumechange attribute

Type:
Develop
Category:
Web Tutorial
Sub Category:
HTML Attribute
Uploaded by:
Admin


Read Article
https://horje.com/learn/1434/reference