HTML Custom Audio Player – Simple Examples

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 Player 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 basic examples of implementing custom controls for HTML audio – No bloat, only the pure mechanics.

 

PART 1) BASIC CUSTOM AUDIO PLAYER

BASIC CUSTOM AUDIO CONTROLS DEMO




 

BASIC PLAYER HTML

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

<!-- (B) CONTROLS -->
<div id="demoControls">
  <!-- (B1) PLAY & PAUSE -->
  <div>
    <input type="button" id="demoPlay" value="Play" disabled/>
    <input type="button" id="demoPause" value="Pause" disabled/>
  </div>
 
  <!-- (B2) SKIP TO TIME -->
  <div>
    <input type="number" id="demoTime" min="0" max="5" value="0" disabled/>
    <input type="button" id="demoSkip" value="Skip To" disabled/>
  </div>
 
  <!-- (B3) SET VOLUME -->
  <div>
    <input type="number" id="demoVol" min="0" max="1" value="1" step="0.1" disabled/>
    <input type="button" id="demoVolume" value="Volume" disabled/>
  </div>
</div>
  1. <audio> is self-explanatory, we enabled controls just to show that the custom buttons work… In your own project, you will want to remove controls and create a hidden audio tag instead.
  2. There are 3 sets of basic controls here.
    • Play and pause.
    • Skip-to-time.
    • Set volume.

 

 

BASIC PLAYER JAVASCRIPT

1b-basic.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
  // (D1) PLAY & PAUSE
  document.getElementById("demoPlay").onclick = function(){ aud.play(); };
  document.getElementById("demoPause").onclick = function(){ aud.pause(); };
 
  // (D2) SKIP TO
  document.getElementById("demoSkip").onclick = function(){
    aud.currentTime = document.getElementById("demoTime").value;
  };
 
  // (D3) SET VOLUME
  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 a “visible HTML <audio> tag” instead – Both methods work 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.

 

 

PART 2) SIMPLE AUDIO PLAYLIST

PLAYLIST HTML

2a-playlist.html
<!-- (A) AUDIO PLAYER -->
<audio id="demoAudio" controls></audio>
<!-- (B) PLAYLIST -->
<div id="demoList"></div>

Sadly, the HTML <audio> does not support a playlist natively. To create a playlist, we will have to manually do so using some Javascript.

 

PLAYLIST JAVASCRIPT

2b-playlist.js
var aud = {
  // (A) PROPERTIES
  list : ["sound1.mp3", "sound2.mp3", "sound3.mp3"], // playlist
  now : 0, // currently playing
  hplay : null, // HTML audio player
  hlist : [], // HTML PLAYLIST
 
  // (B) INIT
  init : function () {
    // (B1) GET HTML AUDIO
    aud.hplay = document.getElementById("demoAudio");
 
    // (B2) GENERATE HTML PLAYLIST
    let wrap = document.getElementById("demoList");
    for (let i in aud.list) {
      let sound = document.createElement("div");
      sound.innerHTML = aud.list[i];
      sound.onclick = aud.play;
      wrap.appendChild(sound);
      aud.hlist.push(sound);
    }
 
    // (B3) AUTOPLAY NEXT SONG IN THE QUEUE
    aud.hplay.onended = function() {
      aud.now++;
      if (aud.now>=aud.list.length) { aud.now = 0; }
      aud.hlist[aud.now].click();
    };
 
    // (B4) SET FIRST SONG/SOUND TO HTML PLAYER
    aud.hplay.src = aud.list[0];
    // aud.player.play();
  },
 
  // (C) PLAY SELECTED FILE
  play : function () {
    aud.hplay.src = this.innerHTML;
    aud.hplay.play();
  }
};
window.addEventListener("DOMContentLoaded", aud.init);

Not going to explain this line-by-line, but the essentials are:

  • A – Define the playlist in an array aud.list = [...], and use a aud.now flag to track the current song.
  • B2 – On window load, we loop through aud.list and create the HTML playlist.
  • B3 – Very important, when the current song ends, we do a aud.now++ and automatically play the next song in the playlist.
  • C – Lastly, a function to start playing the currently selected song.

 

 

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.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Custom HTML Audio Player (click to enlarge)

 

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!

2 thoughts on “HTML Custom Audio Player – Simple Examples”

Leave a Comment

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