HTML Custom Audio Player – Very Simple Example

Welcome to a quick tutorial on how to create an HTML custom audio player. Want to create your own player using HTML audio? Yes, it is possible.

There is no way to change the browser’s default HTML audio interface, but we can create our own custom audio player using Javascript:

  1. Create a new audio object – var aud = new Audio("AUDIO.mp3");
  2. To play – aud.play();
  3. To pause – aud.pause();
  4. Set the volume – aud.volume = 0.0 TO 1.0;
  5. Lastly, to skip to a specified time – aud.currentTime = SECONDS;

That should cover the basics, but read on for more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TABLE OF CONTENTS

Download & Notes Custom Audio Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

CUSTOM AUDIO PLAYER

All right, let us now get into the example of implementing custom controls for HTML audio – No bloat, only the pure mechanics.

 

THE DEMO


 

THE HTML

1-audio.html
<!-- (A) AUDIO TAG -->
<audio src="glitchhop.mp3" id="demoAudio" controls></audio>

<!-- (B) CONTROLS -->
<div id="demoControls">
  <input type="button" id="demoPlay" value="Play" disabled/>
  <input type="button" id="demoPause" value="Pause" disabled/>
  <input type="number" id="demoTime" min="0" max="5" value="0" disabled/>
  <input type="button" id="demoSkip" value="Skip To" disabled/>
  <input type="number" id="demoVol" min="0" max="1" value="1" step="0.1" disabled/>
  <input type="button" id="demoVolume" value="Volume" disabled/>
</div>

Yes, the HTML is that simple.

  1. <audio> is self-explanatory, controls is enabled in this example just to show that the custom buttons work. Otherwise, this audio tag is normally hidden.
  2. There are 4 basic control buttons here. Play, pause, skip-to-time, and volume. Take note that the controls are disabled by default.

 

 

THE JAVASCRIPT

2-audio.js
// (A) GET AUDIO ELEMENT
var aud = document.getElementById("demoAudio");
 
// (B) WAIT FOR SUFFICIENT AUDIO BUFFER
aud.addEventListener("canplay", function(){
  // (C) ENABLE ALL CONTROLS
  for (let i of document.querySelectorAll("#demoControls input")) {
    i.disabled = false;
  }

  // (D) ATTACH CONTROLS
  document.getElementById("demoPlay").onclick = function(){ aud.play(); };
  document.getElementById("demoPause").onclick = function(){ aud.pause(); };
  document.getElementById("demoSkip").onclick = function(){
    aud.currentTime = document.getElementById("demoTime").value;
  };
  document.getElementById("demoVolume").onclick = function(){
    aud.volume = document.getElementById("demoVol").value;
  };
});

This is seemingly confusing at first, but keep calm and read slowly:

  1. In the introduction above, we used var aud = new Audio(). But in this example, we are using an HTML <audio> tag instead – Both methods are correct nonetheless.
  2. Notice the use of aud.addEventListener("canplay")? This is to make sure that the audio is sufficiently buffered before we proceed. Change this to canplaythrough if you want to wait until the audio is fully loaded.
  3. Enable the controls when the audio is ready. Doh.
  4. Attach the audio controls to the custom buttons.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

EXTRA) PLAYLIST

e-playlist.html
<!-- (A) CONTROLS -->
<input type="button" value="Start!" onclick="play.go()"/>
 
<script>
var play = {
  // (B) AUDIO OBJECT, PLAYLIST, CURRENT SONG
  aud : new Audio(),
  list : ["drumloop.mp3", "glitchhop.mp3"],
  current : -1,
 
  // (C) START!
  go : function () {
    // NEXT SONG
    play.current++;
    if (play.current >= play.list.length) { play.current = 0; }

    // PLAY
    play.aud.src = play.list[play.current];
    play.aud.play();
  }
};

// (D) AUTO-START NEXT SONG
play.aud.addEventListener("ended", play.go);
</script>

If you want to create a playlist, the basic idea is to use an array and simply loop through it.

 

INFOGRAPHIC CHEAT SHEET

Custom HTML Audio Player (click to enlarge)

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *