-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
222 lines (177 loc) · 7.78 KB
/
Copy pathscript.js
File metadata and controls
222 lines (177 loc) · 7.78 KB
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
213
214
215
216
217
218
219
220
221
222
// console.log("JavaScript");
let currentSong = new Audio();
let songs;
let currentFolder;
function convertSecondsToMinutes(seconds) {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = Math.floor(seconds % 60);
// Format the remaining seconds to always display two digits
// const formattedSeconds = remainingSeconds < 10 ? `0${remainingSeconds}` : remainingSeconds;
const formattedMinutes = String(minutes).padStart(2, '0');
const formattedSeconds = String(remainingSeconds).padStart(2, '0');
return `${formattedMinutes}:${formattedSeconds}`;
}
async function getSongs(folder) {
currentFolder = folder;
let a = await fetch(`/${folder}/`);
let response = await a.text();
// console.log(response);
let div = document.createElement("div");
div.innerHTML = response;
let list = div.getElementsByTagName("a");
songs = [];
for (let i = 0; i < list.length; i++) {
const element = list[i];
if (element.href.endsWith(".mp3")) {
songs.push(element.href.split(`/${folder}/`)[1]);
}
}
// To show all the song in the playlist
let songUL = document.querySelector(".songsList").getElementsByTagName("ul")[0];
songUL.innerHTML = "";
for (const song of songs) {
songUL.innerHTML = songUL.innerHTML + `<li>
<img src="/images/music.svg" alt="music" class="invert">
<div class="info">
<div>${song.replaceAll("%20", " ")}</div>
<div>Akash Barman</div>
</div>
<div class="playNow">
<span>Play Now</span>
<img src="/images/play.svg" alt="" class="invert">
</div>
</li>`;
}
// Attaching an Event Listener to each song
Array.from(document.querySelector(".songsList").getElementsByTagName("li")).forEach(e => {
e.addEventListener("click", element => {
// console.log(e.querySelector(".info").firstElementChild.innerHTML.replace(".mp3", ""));
playMusic(e.querySelector(".info").firstElementChild.innerHTML);
})
})
// console.log(songs);
return songs;
}
const playMusic = (track, pause = false) => {
currentSong.src = `/${currentFolder}/` + track;
if (!pause) {
currentSong.play();
play.src = "images/pause.svg";
}
document.querySelector(".songInfo").innerHTML = track.replace(".mp3", "").replaceAll("%20", " ");
currentSong.addEventListener("loadedmetadata", () => {
const totalDuration = convertSecondsToMinutes(currentSong.duration);
document.querySelector(".songTime").innerHTML = `00:00 / ${totalDuration}`;
});
}
async function displayAlbums() {
let a = await fetch(`/songs/`);
let response = await a.text();
let div = document.createElement("div");
div.innerHTML = response;
// console.log(div);
let anchors = div.getElementsByTagName("a");
let cardContainer = document.querySelector(".cardContainer");
let array = Array.from(anchors);
for (let index = 0; index < array.length; index++) {
const e = array[index];
let parts = e.href.split(`/songs/`);
if (parts.length > 1) {
let folder = parts[1].replaceAll("%20", " ");
// console.log(folder);
let a = await fetch(`/songs/${folder}/info.json`);
let response = await a.json();
// console.log(response);
cardContainer.innerHTML = cardContainer.innerHTML + `<div data-folder="${folder}" class="card">
<div class="play"><img src="/images/hover-play.svg" alt=""></div>
<img src="/songs/${folder}/cover.jpg" alt="">
<h2>${response.title}</h2>
<p>${response.description}</p>
</div>`
}
};
// Load Playlist when Card is Clicked
Array.from(document.getElementsByClassName("card")).forEach(e => {
e.addEventListener("click", async item => {
// console.log(`songs/${(item.currentTarget.dataset.folder).replace(" ", "%20")}`)
songs = await getSongs(`songs/${(item.currentTarget.dataset.folder).replace(" ", "%20")}`);
playMusic(songs[0]);
})
})
}
async function main() {
// To get the list of songs
await getSongs("songs/ALBUM");
// console.log(songs);
playMusic(songs[0], true)
// Display all the Albums on Page
displayAlbums();
// Attaching an Event Listener to play & pause song
play.addEventListener("click", () => {
if (currentSong.paused) {
currentSong.play();
play.src = "images/pause.svg";
} else {
currentSong.pause();
play.src = "images/play.svg";
}
})
// Music duration real time update
currentSong.addEventListener("timeupdate", () => {
// console.log(currentSong.currentTime, currentSong.duration);
document.querySelector(".songTime").innerHTML = `${convertSecondsToMinutes(currentSong.currentTime)} / ${convertSecondsToMinutes(currentSong.duration)}`;
document.querySelector(".circle").style.left = (currentSong.currentTime / currentSong.duration) * 100 + "%";
})
// Adding Event Listener to SeekBar
document.querySelector(".seekbar").addEventListener("click", e => {
let pinDuration = (e.offsetX / e.target.getBoundingClientRect().width) * 100;
document.querySelector(".circle").style.left = pinDuration + "%";
currentSong.currentTime = (currentSong.duration * pinDuration) / 100;
})
// Adding Event Listener to Previous Song
prevsong.addEventListener("click", () => {
let index = songs.indexOf(currentSong.src.split('/').slice(-1)[0]);
if (index - 1 < 0) {
// If the first song is currently playing, play the last song
playMusic(songs[songs.length - 1]);
} else {
// Play the previous song
playMusic(songs[index - 1]);
}
})
// Adding Event Listener to Next Song
nextsong.addEventListener("click", () => {
let index = songs.indexOf(currentSong.src.split('/').slice(-1)[0]);
if (index + 1 >= songs.length) {
// If the last song is currently playing, play the first song
playMusic(songs[0]);
} else {
// Play the next song
playMusic(songs[index + 1]);
}
})
// Add an Event Listner to Hamburger Menu
document.querySelector(".hamburger").addEventListener("click", () => {
document.querySelector(".left").style.left = "0";
document.querySelector(".close").style.display = "flex";
document.querySelector(".close").addEventListener("click", () => {
document.querySelector(".left").style.left = "-100%";
})
})
// Add an Event Listner to Adjust Volume
document.querySelector("#range").addEventListener("change", (e) => {
// console.log(e.target.value);
currentSong.volume = parseInt(e.target.value) / 100;
})
// Add an Event Listner to Volume
document.querySelector("#volume").addEventListener("click", e=>{
if(e.target.src.includes("volume.svg")){
e.target.src = e.target.src.replace("volume.svg", "mute.svg");
document.querySelector("#range").value = 0;
} else{
e.target.src = e.target.src.replace("mute.svg", "volume.svg");
document.querySelector("#range").value = 50;
}
})
}
main();