JavaScript Button to control Audio Elements

<a href="#audio" type="button" onclick="aud_play_pause()">Play/Pause</a>

<audio volume="0.1" id="myAudio" autoplay loop>
  <source src="/audio.mp3" type="audio/mpeg" />
  <source src="/audio.ogg" type="audio/ogg" />
  <source src="/audio.wav" type="audio/wav" />
</audio>

<script>
function aud_play_pause() {
  var myAudio = document.getElementById("myAudio");
  if (myAudio.paused) {
    myAudio.play();
  } else {
    myAudio.pause();
  }
}
</script>

Leave a Reply