Javascript DOM onplay Event on <audio> element

Introduction

Execute a JavaScript when an audio has started to play:

This example demonstrates how to assign an "onplay" event to an audio element.

View in separate window

<!DOCTYPE html>
<html>
<body>
<p>Press play.</p>

<audio controls onplay="myFunction()">
  <source src="sound.ogg" type="audio/ogg">
  <source src="sound.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>/*from w  w w  .  j av  a  2  s  . com*/

<p id="demo"></p>
<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "The audio started to play";
}
</script>

</body>
</html>



PreviousNext

Related