-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
165 lines (107 loc) · 4.38 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
console.log("Let's Write some JavScript")
let currentSong = new Audio(); //Global Varible...and it is made an object so that one time only one song will be played
//Function to change secods to minutes
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60); // Get the whole minutes
const remainingSeconds = Math.floor(seconds % 60); // Get the remaining seconds (rounded down)
// Format with leading zero if less than 10
const formattedMinutes = minutes.toString().padStart(2, '0');
const formattedSeconds = remainingSeconds.toString().padStart(2, '0');
return `${formattedMinutes}:${formattedSeconds}`;
}
//Function to fetch the songs from the API
async function getSongs() {
let a = await fetch("/songs/")
let response = await a.text()
console.log(response)
let div = document.createElement("div")
div.innerHTML=response;
let as = div.getElementsByTagName("a")
let songs = []
for (let index = 0; index < as.length; index++) {
let element = as[index];
if(element.href.endsWith(".mp3"))
{
songs.push(element.href.split("/songs/")[1])
}
}
return songs;
}
//Function that defines the things happen after I play a song
const playMusic = (track ,pause=false)=>{
// let audio = new Audio("/songs/" + track);
currentSong.src = "/songs/" + track;
if(!pause)
{
currentSong.play()
play.src="pause.svg";
}
document.querySelector(".songinfo").innerHTML =decodeURI(track)
document.querySelector(".songtime").innerHTML ="00:00 / 00:00"
}
async function main()
{
//Get the list of all the songs
let songs = await getSongs()
//Setting a song by defualt to play
playMusic(songs[0],true)
// console.log(songs)
//Show all the songs in the playlist
let songUL = document.querySelector(".songlist").getElementsByTagName("ul")[0]
for (const song of songs) {
songUL.innerHTML = songUL.innerHTML + `<li>
<img class="invert" src="music.svg" alt="">
<div class="info">
<div> ${song.replaceAll("%20" ," ")} </div>
<div>Jeet Shee</div>
</div>
<div class="playnow">
<span>Play Now</span>
<img class="invert" src="play.svg" alt="">
</div>
</li>`;
}
//Attatch an Event Listener to Each Song
Array.from(document.querySelector(".songlist").getElementsByTagName("li")).forEach(element => {
element.addEventListener("click",()=>{
console.log(element.querySelector(".info").firstElementChild.innerHTML)
playMusic(element.querySelector(".info").firstElementChild.innerHTML.trim());
})
});
//Attatch an Event Listener to the Play ,previous and next Now Button
play.addEventListener("click",()=>{
if(currentSong.paused)
{
currentSong.play()
play.src="pause.svg"
}
else
{
currentSong.pause()
play.src="play.svg"
}
})
//Listen for time update event
currentSong.addEventListener("timeupdate", () => {
//Update the progress bar
document.querySelector(".songtime").innerHTML=`${formatTime(currentSong.currentTime)}/${formatTime(currentSong.duration)}`
console.log(currentSong.currentTime,currentSong.duration)
document.querySelector(".circle").style.left=((currentSong.currentTime/currentSong.duration)*100) + "%";
});
//Seek Event Listener
document.querySelector(".seekbar").addEventListener("click",(e)=>{
let percent=((e.offsetX/e.target.getBoundingClientRect().width)*100)
document.querySelector(".circle").style.left=percent + "%";
currentSong.currentTime=(currentSong.duration*percent)/100;
})
//Add EventListener to hamburger
document.querySelector(".hamburger").addEventListener("click",()=>{
document.querySelector(".left").style.left="0";
})
//Add EventListener to close
document.querySelector(".close").addEventListener("click",()=>{
document.querySelector(".left").style.left= "-120%" ;
});
//Add Event Listener for previous Button
}
main()