<! DOCTYPEhtml>
<html lang="en">

<head>
  <title>Js controls the HTML5 Audio tag to play audio files</title>
</head>

<body>
  <button onclick="play()">Playback of audio</button>
  <button onclick="pause()">Pause the audio</button>
  <button onclick="stop()">Stop the audio</button>
  <button onclick="skip()">Skip to 10 seconds</button>
  <script type="text/javascript">
    var audio = document.createElement("audio");
    audio.src = "alarm.mp3";
    audio.controls = true

    document.body.append(audio)
    function play() {
      audio.play();
    }
    function pause() {
      audio.pause();
    }
    function stop() {
      audio.currentTime = 0;
      audio.pause();
    }
    function skip() {
      audio.currentTime = 10;
      audio.play();
    }
  </script>
</body>

</html>
Copy the code