Javascript DOM HTML Audio play() Method

Introduction

An audio player with play and pause buttons:

Click the buttons to play or pause the audio.

View in separate window

<!DOCTYPE html>
<html>
<body>

<audio id="myAudio">
  <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 ww.  java 2s  .  co m
<button onclick="playAudio()" type="button">Play Audio</button>
<button onclick="pauseAudio()" type="button">Pause Audio</button> 

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

function playAudio() { 
  x.play(); 
} 

function pauseAudio() { 
  x.pause(); 
} 
</script>

</body>
</html>

The play() method starts playing the current audio.




PreviousNext

Related