Skip to content

Commit faf8fe4

Browse files
committed
Adding Day #11
1 parent f10c26c commit faf8fe4

File tree

13 files changed

+301
-3
lines changed

13 files changed

+301
-3
lines changed

Day #11 - Music Player App/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Day #11
2+
3+
### Music Player App
4+
In this tutorial ([Open in Youtube](https://youtu.be/5y_P2ZGLarE)), I am gonna showing to you how to code a music player in javascript. we create a project that you can play audio files with javascript from local folder or any url❗️
5+
6+
# Screenshot
7+
Here we have project screenshot :
8+
9+
![screenshot](ScreenShot.png)
528 KB
Loading
3.86 MB
Loading
2.93 MB
Binary file not shown.
4.9 MB
Loading
16.8 MB
Binary file not shown.
281 KB
Loading
2.56 MB
Binary file not shown.

Day #11 - Music Player App/index.html

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<!---------------- Font Awesome ----------------->
7+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
8+
<!------------------ CSS ----------------->
9+
<link rel="stylesheet" href="style.css">
10+
<!-- ------------------------------------------ -->
11+
<title>Day #11 - Music Player App | NaeuCode</title>
12+
</head>
13+
<body>
14+
<div class="background">
15+
<img src="" id="bg-img">
16+
</div>
17+
18+
<div class="container">
19+
<div class="player-img">
20+
<img src="" class="active" id="cover">
21+
</div>
22+
23+
<h2 id="music-title"></h2>
24+
<h3 id="music-artist"></h3>
25+
26+
<div class="player-progress"id="player-progress">
27+
<div class="progress" id="progress"></div>
28+
<div class="music-duration">
29+
<span id="current-time">0:00</span>
30+
<span id="duration">0:00</span>
31+
</div>
32+
</div>
33+
34+
<div class="player-controls">
35+
<i class="fa-solid fa-backward" title="Pervious" id="prev"></i>
36+
<i class="fa-solid fa-play play-button" title="Play" id="play"></i>
37+
<i class="fa-solid fa-forward" title="Next" id="next"></i>
38+
39+
</div>
40+
41+
</div>
42+
43+
44+
<script src="index.js"></script>
45+
</body>
46+
</html>

Day #11 - Music Player App/index.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
const image = document.getElementById("cover");
2+
const title = document.getElementById("music-title");
3+
const artist = document.getElementById("music-artist");
4+
const currentTimeEl = document.getElementById("current-time");
5+
const durationEl = document.getElementById("duration");
6+
const progress = document.getElementById("progress");
7+
const playerProgress = document.getElementById("player-progress");
8+
const prevBtn = document.getElementById("prev");
9+
const nextBtn = document.getElementById("next");
10+
const playBtn = document.getElementById("play");
11+
const background = document.getElementById("bg-img");
12+
13+
const music = new Audio();
14+
15+
const songs = [
16+
{
17+
path: "assets/1.mp3",
18+
displayName: "Tokyo Cafe",
19+
cover: "assets/1.jpg",
20+
artist: "TVARI",
21+
},
22+
{
23+
path: "assets/2.mp3",
24+
displayName: "The Long Dark",
25+
cover: "assets/2.jpg",
26+
artist: "Naeu",
27+
},
28+
{
29+
path: "assets/3.mp3",
30+
displayName: "Justhea",
31+
cover: "assets/3.jpg",
32+
artist: "Summer Breeze",
33+
},
34+
];
35+
36+
let musicIndex = 0;
37+
let isPlaying = false;
38+
39+
function togglePlay() {
40+
if (isPlaying) {
41+
pauseMusic();
42+
} else {
43+
playMusic();
44+
}
45+
}
46+
47+
function playMusic(){
48+
isPlaying = true;
49+
playBtn.classList.replace("fa-play", "fa-pause");
50+
playBtn.setAttribute("title", "Pause");
51+
music.play();
52+
}
53+
54+
function pauseMusic(){
55+
isPlaying = false;
56+
playBtn.classList.replace("fa-pause", "fa-play");
57+
playBtn.setAttribute("title", "Play");
58+
music.pause();
59+
}
60+
61+
function loadMusic(song) {
62+
music.src = song.path;
63+
title.textContent = song.displayName;
64+
artist.textContent = song.artist;
65+
image.src = song.cover;
66+
background.src = song.cover;
67+
}
68+
69+
function changeMusic(direction) {
70+
musicIndex = (musicIndex + direction + songs.length) % songs.length;
71+
loadMusic(songs[musicIndex]);
72+
playMusic();
73+
}
74+
75+
function updateProgressBar(){
76+
const { duration, currentTime } = music;
77+
const progressPercent = (currentTime / duration) * 100;
78+
progress.style.width = `${progressPercent}%`;
79+
80+
const formatTime = (time) => String(Math.floor(time)).padStart(2, "0");
81+
durationEl.textContent = `${formatTime(duration / 60)}: ${formatTime(duration % 60)}`;
82+
currentTimeEl.textContent = `${formatTime(currentTime / 60)}:${formatTime(currentTime % 60)}`;
83+
}
84+
85+
function setProgressBar(e) {
86+
const width = playerProgress.clientWidth;
87+
const clickX = e.offsetX;
88+
music.currentTime = (clickX / width) * music.duration;
89+
}
90+
91+
playBtn.addEventListener("click", togglePlay);
92+
prevBtn.addEventListener("click", () => changeMusic(-1));
93+
nextBtn.addEventListener("click", () => changeMusic(1));
94+
music.addEventListener("ended", () => changeMusic(1));
95+
playerProgress.addEventListener("click", setProgressBar );
96+
music.addEventListener("timeupdate", updateProgressBar);
97+
98+
loadMusic(songs[musicIndex]);

0 commit comments

Comments
 (0)