YO YO! CUSTOM MUSIC PLAYER FOR FREE???
I've been hard at work to make my web-shrine for
The Beatles and wanted to have a music player for the page, but I couldn't find any good ones on the net, so I resorted to making one myself. It took way too long and a bit of
copying and
pasting from other music players that work because I know next-to-nothing about Javascript, but I got it running! Take a look:
https://schlime.online/music-player/I'll run through some of the HTML and Javascript. The CSS is done but you should be able to make it look however you want, it doesn't
HAVE to look like something made in the late 90's...
Step #1. Da HTML<div id="player">
<div class="container">
<div id="player-art">
<img id="cover" src="">
</div>
<div class="inner-container">
<div class="container">
<div class="track-title">
<p id="now-playing"></p>
</div>
<button onclick="myFunction()" class="dropdown-button">▼</button>
</div>
<div id="dropdown" class="dropdown-content">
<a href="#" onclick="selectalbum('[Title of Album]'); return false">Album 1</a>
<a href="#" onclick="selectalbum('[Title of Album 2]'); return false">Album 2</a>
</div>
Here is the main part of the whole player, you have the album art that has the id of
"cover" which is intentionally left blank as to let the javascript put the url of the currently loaded album that you select into the
href area. Same thing with the
"now-playing" p. In the
"dropdown" area, you can have as many dropdowns as you want. The
"return false" is very important! For some reason it the button doesn't work unless it's there, at least from my experience.
<div class="playback">
<button onclick="prevtrack()" title="Previous Track">⏮</button>
<button id="play" onclick="playback()" title="Play / Pause">▶</button>
<button onclick="nexttrack()" title="Next Track">⏭</button>
</div>
<input id="scrubber" type="range" min="0" max="0" value="0" step="1">
<div id="runtime">
<span id="current-time">0:00</span>
<span id="duration">0:00</span>
</div>
These are the buttons to play/pause, skip and go back, and also the scrubber. Nothing too crazy here except this
"▶" thingy... What is that?
Why it's HTML entities of course! Basically, symbols and shapes in HTML need to be written as strings of symbols and numbers such as ones you see above. I use them all the time to place n-dashes (written as
–) and ampersands (written as
&). It'll be the same with playback symbols like you see above. That's why it's important for us in the Javascript section to use "
innerHTML" instead of the more common "
textContent".
The scrubber's pretty simple, and same with the time and duration stuff, make sure to use those exact ID's, they don't need to be
<span>s, but I like changing up what kind of text tag I use every now and then.
Step #2. javascript... >:(You probably can't tell, but I don't like Javascript... But alas, I kinda have to use it.
const albums = {
"[Title of Album]": {
cover: "../images/bgs/clouds.png",
tracks: [
{ title: "Track 01", src: "link-2-track-1-here" },
{ title: "Track 02", src: "link-2-track-2-here" },
{ title: "Track 03", src: "you get the idea.mp3" }
]
},
etc...
This is where you'll put all your music. I labelled it as
"albums" because that's how I'm using my music player, but you can put playlists or just single songs on there, it doesn't matter. It also doesn't matter how you format this section, this is just easy on the eyes for me but format it however you like. If you put more than one album/playlist here,
make sure the comma is there at the last curly bracket, and if it's the last album, get rid of the comma! It does break everything so yes, it is important!
// all constants you need to make this -- make sure the IDs match
const audio = document.getElementById("music-player");
const cover = document.getElementById("cover");
const nowplaying = document.getElementById("now-playing");
const playbutton = document.getElementById("play");
const scrubber = document.getElementById("scrubber");
const currenttime = document.getElementById("current-time");
const duration = document.getElementById("duration");
let currentalbum = "[Title of Album]"; // the album / playlist the page will open on
let currenttrack = 0; // computer thing, track 0 is actually track 1, you get the idea
You can change the IDs if you want, it's not a
breaks everything thing but make sure you update the IDs everywhere else if you do change them.
function loadtrack() {
const track = albums[currentalbum].tracks[currenttrack];
audio.src = track.src;
cover.src = albums[currentalbum].cover;
nowplaying.innerHTML = "Now Playing — " + track.title;
scrubber.value = 0;
currenttime.innerHTML = "0:00";
duration.innerHTML = "0:00";
}
Everything else imo is pretty self-explanatory by simply looking at the code, but I'll still explain it anyway. This simply loads all the relevant information for what the player needs to show, like what track it is, displaying the "now playing" section, cover, etc.
function formatTime(time) {
if (!isFinite(time)) return "0:00";
const minutes = Math.floor(time / 60);
const seconds = Math.floor(time % 60);
return minutes + ":" + String(seconds).padStart(2, "0");
}
audio.ontimeupdate = function() {
currenttime.innerHTML = formatTime(audio.currentTime);
scrubber.value = audio.currentTime;
};
audio.onloadedmetadata = function() {
scrubber.max = audio.duration;
duration.innerHTML = formatTime(audio.duration);
};
scrubber.oninput = function() {
audio.currentTime = scrubber.value;
};
This is everything to do with the scrubber and also time/duration stuff. I stole this from somewhere, I forgot where though. I thought I'd put a note in my html about where I got it from but apparently not. Nonetheless, this is not my code.
Step #3. Ctrl+C, Ctrl+V!Yup! Simply copy and paste the below attachments into your webpage and make the player your own! Hope you enjoy, and do tell us in the comments of this thread if you upgraded the player and how! I'd love to have maybe a "track selector" or have sections in the dropdown to show either playlists or albums!
Cya!
- schlime <3