-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
212 lines (183 loc) · 6.72 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
let currentsong= new Audio();
let song;
let currFolder;
let vol;
function secondsToMinutesSeconds(seconds) {
if (isNaN(seconds) || seconds < 0) {
return "00:00";
}
const minutes = Math.floor(seconds / 60);
const remainingSeconds = Math.floor(seconds % 60);
const formattedMinutes = String(minutes).padStart(2, '0');
const formattedSeconds = String(remainingSeconds).padStart(2, '0');
return `${formattedMinutes}:${formattedSeconds}`;
}
async function getsongs(folder){
currFolder=folder
let a = await fetch(`${currFolder}/`)
let response = await a.text()
let div = document.createElement("div")
div.innerHTML=response;
let links=div.getElementsByTagName("a")
let songs=[]
for (let index = 0; index < links.length; index++) {
const element = links[index];
if(element.href.endsWith(".mp3")){
songs.push(element.href.split(`${currFolder}/`)[1])
}
}
let songul= document.querySelector(".songlist").getElementsByTagName("ul")[0]
songul.innerHTML=""
playMusic(songs[0],true)
for (const song of songs) {
let a1= await fetch(`${currFolder}/metadata.json`)
let r1= await a1.json()
let artist1
for(i=0;i<r1.length;i++){
if(r1[i].title==song.replaceAll("%20"," ").replaceAll(".mp3","")){artist1=r1[i].artist
}
}
songul.innerHTML=songul.innerHTML+`
<li><img class="invert" src="icons/music.svg" alt="">
<div class="info">
<div >${song.replaceAll("%20"," ").replaceAll(".mp3","")}</div>
<div>${artist1}</div>
</div>
<div class="playnow">
<span>Play Now</span>
<img class="invert" src="icons/play.svg" alt="">
</div>
</li>`;
}
Array.from(document.querySelector(".songlist").getElementsByTagName("li")).forEach(e => {
e.addEventListener("click",element=>{
playMusic(e.querySelector(".info").firstElementChild.innerHTML.trim())
})
})
return songs
}
const playMusic=(track,pause=false)=>{
if(!track.endsWith(".mp3")){
currentsong.src=`${currFolder}/`+ track +".mp3"
}
else{
currentsong.src=`${currFolder}/`+ track
}
if(!pause){
currentsong.play()
play.src="icons/pause.svg"
}
document.querySelector(".songinfo").innerHTML=track.replaceAll(".mp3","").replaceAll("%20"," ")
document.querySelector(".songtime").innerHTML=`${secondsToMinutesSeconds(currentsong.currentTime)}/${secondsToMinutesSeconds(currentsong.duration)}`
}
async function displayalbums(){
let a = await fetch(`Songs`)
let response = await a.text()
let div = document.createElement("div")
let cardcontainer = document.querySelector(".card-container")
div.innerHTML=response;
let anchors = div.getElementsByTagName("a")
let array=Array.from(anchors)
for (let index = 0; index < array.length; index++) {
const e = array[index];
if(e.href.includes("/Songs")){
let folder=e.href.split(`/`).slice(-2)[0]
if(folder!="Songs"){
let a = await fetch(`Songs/${folder}/info.json`)
let response = await a.json()
cardcontainer.innerHTML = cardcontainer.innerHTML + ` <div data-folder="${folder}" class="card">
<div class="play">
<img src="icons/play-button-green-icon.svg" alt="">
</div>
<img src="/Songs/${folder}/cover.jpg" alt="${response.Description}">
<h5>${response.heading}
</h5>
<p>${response.Description}</p>
</div>`}
}
}
Array.from(document.getElementsByClassName("card")).forEach(e=>{
e.addEventListener("click",async item=>{
song = await getsongs(`Songs/${item.currentTarget.dataset.folder}`)
playMusic(song[0],true)
if(play.src="icons/pause.svg"){
play.src="icons/play.svg"
}
})
})
}
async function main(){
song= await getsongs("Songs/JD")
playMusic(song[0],true)
displayalbums()
play.addEventListener("click",e=>{
if(currentsong.paused){
currentsong.play()
play.src="icons/pause.svg"
}
else{
currentsong.pause();
play.src="icons/play.svg"
}
})
currentsong.addEventListener("timeupdate",()=>{
document.querySelector(".songtime").innerHTML=`${secondsToMinutesSeconds(currentsong.currentTime)} / ${secondsToMinutesSeconds(currentsong.duration)}`
document.querySelector(".circle").style.left=((currentsong.currentTime/currentsong.duration)*100)-1+"%"
})
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
})
document.querySelector(".hamburger").addEventListener("click",()=>{
document.querySelector(".left").style.left="0%"
})
document.querySelector(".close").addEventListener("click",()=>{
document.querySelector(".left").style.left="-150%"
})
prev.addEventListener("click",()=>{
let folder=currentsong.src.split("/").slice(-2)[0]
let index=song.indexOf(currentsong.src.split(`/${folder}/`)[1])
if([index-1]>=0){
playMusic(song[index-1].replaceAll("%20"," "))
}
})
next.addEventListener("click",()=>{
let folder=currentsong.src.split("/").slice(-2)[0]
let index=song.indexOf(currentsong.src.split(`/${folder}/`)[1])
if([index+1]<song.length){
playMusic(song[index+1].replaceAll("%20"," "))
}
})
document.querySelector(".slider").addEventListener("change",(e)=>{
vol=e.target.value;
currentsong.volume=parseInt(e.target.value)/100
})
Array.from(document.getElementsByClassName("card")).forEach(e=>{
e.addEventListener("click",async item=>{
song = await getsongs(`Songs/${item.currentTarget.dataset.folder}`)
})
})
document.querySelector(".vol").addEventListener("click",(e)=>{
if(e.target.src.includes("volume.svg")){
e.target.src=e.target.src.replace("volume.svg","mute.svg")
currentsong.volume=0;
document.querySelector(".slider").value = 0;
}else{
e.target.src=e.target.src.replace("mute.svg","volume.svg")
currentsong.volume=0.5;
document.querySelector(".slider").value = 50
}
})
function pn(){
if(!currentsong.paused && secondsToMinutesSeconds(currentsong.duration)!="00:00"){
if(secondsToMinutesSeconds(currentsong.currentTime)==secondsToMinutesSeconds(currentsong.duration)){
let folder=currentsong.src.split("/").slice(-2)[0]
let index=song.indexOf(currentsong.src.split(`/${folder}/`)[1])
if([index+1]<song.length){
playMusic(song[index+1].replaceAll("%20"," "))
}}}
}
setInterval(pn,1000)
}
main()