Very Simple PHP Audio Player (With Playlist)

Welcome to a tutorial on how to create a simple PHP audio player. Want to create your own podcast or playlist? Once upon a time, we have to fight with digital dragons and use all sorts of plugins to do that. But today, it is nearly as simple as using an <audio> tag. Read on for the example!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

PHP AUDIO PLAYER

All right, let us now get into how to create a PHP audio player with a playlist.

 

 

 

STEP 1) THE PHP & HTML

audio.php
<div id="demo">
  <!-- (A) AUDIO TAG -->
  <audio id="demoAudio" controls></audio>
 
  <!-- (B) PLAYLIST -->
  <div id="demoList"><?php
    // (B1) GET ALL SONGS
    $songs = glob("audio/*.{mp3,webm,ogg,wav}", GLOB_BRACE);
 
    // (B2) OUTPUT SONGS IN <DIV>
    if (is_array($songs)) { foreach ($songs as $k=>$s) {
      $name = basename($s);
      printf("<div data-src='%s' class='song'>%s</div>", rawurlencode($name), $name);
    }} else { echo "No songs found!"; }
  ?></div>
</div>

Yep, there are only 2 components for this simple example.

  1. The audio player, good old <audio> tag.
  2. The playlist.
    • We will use $songs = glob("audio/*.{mp3,webm,ogg,wav}", GLOB_BRACE) to get the list of songs from the audio folder. Take note, glob will not recursively read into sub-folders.
    • Then, loop through $songs and generate the playlist. Take note of the custom data-src here.

 

 

STEP 2) JAVASCRIPT PLAYLIST INIT

audio.js
var aud = {
  // (A) INITIALIZE PLAYER
  player : null,   // html <audio> element
  playlist : null, // html playlist
  now : 0,         // current song
  init : () => {
    // (A1) GET HTML ELEMENTS
    aud.player = document.getElementById("demoAudio");
    aud.playlist = document.querySelectorAll("#demoList .song");
 
    // (A2) LOOP THROUGH ALL THE SONGS, CLICK TO PLAY
    for (let i=0; i<aud.playlist.length; i++) {
      aud.playlist[i].onclick = () => aud.play(i);
    }
 
    // (A3) AUTO PLAY WHEN SUFFICIENTLY LOADED
    aud.player.oncanplay = aud.player.play;
 
    // (A4) AUTOPLAY NEXT SONG IN PLAYLIST WHEN CURRENT SONG ENDS
    aud.player.onended = () => {
      aud.now++;
      if (aud.now>=aud.playlist.length) { aud.now = 0; }
      aud.play(aud.now);
    };
  }
};
window.addEventListener("DOMContentLoaded", aud.init);

Unfortunately, <audio> does not support playlists at the time of writing. So we will need to build our own, var aud contains the mechanics of our custom playlist player.

  • (A1) Get the HTML elements.
    • aud.player refers to <audio id="demoAudio">.
    • aud.playlist is an HTMLCollection of songs contained in <div id="demoList">.
    • The idea is to use aud.now to track the current song playing in aud.playlist. For example, aud.now = 0 will indicate the first song, aud.now = 1 indicates the second, and so on.
  • (A2) Loop through all the songs, attach “click to play” onclick = aud.play.
  • (A3) Just a bit of precaution here, we wait until the song is sufficiently buffered before playing.
  • (A4) When the current song ends, we do a aud.now++ and play the next song automatically.

 

 

STEP 3) PLAY SONG

audio.js
// (B) START PLAYING
aud.play : id => {
  // (B1) UPDATE CURRENT & PLAY
  aud.now = id;
  aud.player.src = "audio/" + aud.playlist[id].dataset.src;
 
  // (B2) A LITTLE BIT OF COSMETIC
  for (let i=0; i<aud.playlist.length; i++) {
    if (i==id) { aud.playlist[i].classList.add("now"); }
    else { aud.playlist[i].classList.remove("now"); }
  }
};

Lastly, we have a function that plays the song itself. Very simply, we set aud.play.src to the selected data-src. The end. aud.player.oncanplay will automatically start playing once sufficiently buffered.

 

EXTRAS

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

 

CUSTOMIZING THE AUDIO PLAYER?

Take extra note that <audio> cannot be styled or customized (at the time of writing). But we can build one ourselves, check out the custom audio player link below.

 

 

COMPATIBILITY CHECKS

Works on all modern “Grade A” browsers.

 

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!

35 thoughts on “Very Simple PHP Audio Player (With Playlist)”

  1. Wow very nice script. Question – is it possible to add a random “shuffle” feature useable after playlist is loaded?

    Thanks!

    1. Yes. Add your own aud.shuffle flag. aud.player.onended - if (aud.shuffle) { RANDOM } else { NEXT SONG AS USUAL }.

  2. Awesome script!
    Just a little bug: it cannot play a song with a single quote (‘) in the filename
    Please help! Appreciate!

  3. Nice script, one question: Is it possible to make the songs downloadable. I used a similar script which echoes the songs as links but my player-solution was horrible. Your script finally solves my player-issue but now I am missing a download function. Any ideas or tweaks?

  4. Just for clarification, this is just PHP and JavaScript, not dependent to your Core Boxx – PHP Modular Development Framework, right?

    1. Sometimes, people ask the strangest things on planet Earth… I am sure the framework is not even mentioned in the tutorial.

Leave a Comment

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