I am currently working on building a music player from scratch using HTML, CSS, and JavaScript only. To store the list of songs, I have created an array named "songs" with details such as song name, file path, and cover image path.
let songs = [
{songName: "Butter", filePath:"songs/1.mp3",coverPath:'covers/1.png'},
{songName: "Boy With Luv", filePath:"songs/2.mp3",coverPath:'covers/2.jpeg'},
{songName: "Dynamite", filePath:"songs/3.mp3",coverPath:'covers/3.jpeg'},
{songName: "Idol", filePath:"songs/4.mp3",coverPath:'covers/4.png'},
{songName: "Life Goes On", filePath:"songs/5.mp3",coverPath:'covers/5.jpeg'},
{songName: "Mic Drop", filePath:"songs/6.mp3",coverPath:'covers/6.jpeg'},
]
Here is how the user interface looks:
https://i.stack.imgur.com/0Uk8l.png
Currently, when changing to the next song using the navigation buttons at the bottom, the song changes but the play icon does not update accordingly. For example, if you switch to the fourth song, the UI still shows the third song playing.
https://i.stack.imgur.com/RFswd.png
I want to modify the styling of the icon when either the next or previous button is clicked. The function has access to the current song's index for reference.
For your convenience, here is the code snippet related to the next button functionality:
//next button
document.getElementById('next').addEventListener('click', (e)=>{
songIndex = (songIndex >=5) ? 0 : songIndex+1;
audioElement.src = `songs/${songIndex}.mp3`;
audioElement.currentTime = 0;
audioElement.play();
masterPlay.classList.remove('fa-play-circle');
masterPlay.classList.add('fa-pause-circle');
mastersongName.innerText = "BTS - "+songs[songIndex].songName;
masterSideImg.src = songs[songIndex].coverPath;
})
Below is the complete HTML structure of the project:
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Redify - listen music here</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<nav>
<ul>
<li class="brand"><img src="logo.png" alt="logo">Spotify</li>
<li>Home</li>
<li>About</li>
</ul>
</nav>
<div class="container">
<div class="songList">
<h1>Best of BTS</h1>
<!-- Songs List Items Go Here -->
</div>
<div class="songBanner">
<!-- Song Banner Content Goes Here -->
</div>
</div>
<div class="bottom">
<!-- Bottom Controls Section -->
</div>
<script src="https://kit.fontawesome.com/06646b7200.js" crossorigin="anonymous"></script>
<script src="script.js"></script>
You can visit the GitHub repository for the full project implementation: https://github.com/mohitm15/Redify