DIY Audio Player
One reason I decided to try and make my own audio player for my website was that I wanted it to look and behave a certain way that fit in with my website; another reason is because it has better privacy and security implications to use self-hosted scripts. Luckily, it's not too hard, and I figured it out so you don't have to!
A few things you need to set up:
- HTML scaffolding for the player
- Some styling (can be minimal, can be fancy; there's a minimal snippet I'll show that you can build off of)
- A folder called
playlist
that contains your audio files and a file calledplaylist.json
; - A bit of javascript that's kind of long but not actually that scary due to my commitment to writing it in plain javascript and not in jQuery.
<div id="player-wrapper">
<div id="song-info">
<div id="song-image"></div>
<div id="song-labels">
<div id="song-name"></div>
<div id="song-artist"></div>
<div id="song-album"></div>
</div>
</div>
<div id="playback-position"><div id="playback-marker"></div><span id="position-text"></span></div>
<button id="play-pause" data-playing="false" role="switch" aria-checked="false">
<span>Play</span>
</button>
<audio src="" id="music-player"></audio>
</div>
(I borrowed the HTML for the button from this tutorialon making a music player leveraging the YouTube API)
So that's the HTML scaffolding; pretty much everything is prominently labeled. If you wanted to add more fields, you could put them in the box with the id #song-labels
Next, we'll need to add a little CSS to make things look right. You can adapt this a lot, of course, and put your own spin on it! I've only included the barebones stuff.
#playlist-wrapper {
position: relative;
}
#playback-position {
width: 100%;
height: 1px;
position: relative;
background-color: black;
}
#playback-marker {
width: 6px;
height: 6px;
border-radius: 100%;
background-color: black;
transform: translateX(-50%) translateY(-50%);
}
This will not look good! I am confident in your ability to make it look nicer, though. After all, this is Neocities. You're here because you're cool.
Then, let's go make our playlist file. We're going to use a file format called JSON that's a file containing a JavaScript object. Here's an example:
[
{
"name": "song name",
"artist": "artist name",
"album_art": "path/to/album/art.jpg";,
"file": "path/to/song_file.ogg";,
"credit": "https://credit.link";
}
]
To add more items, add a comma after the closing curly brace, and copy the curly-braced section. As the script is currently set up, you'll want your music files and cover art images in the same folder, but you can change that up with pretty minimal edits. Playlist items are played in order from top to bottom.
Last, this is the javascript snippet you need to add. You can either paste it into a script tag or put it in a separate file; just put it right before the closing