-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.js
More file actions
230 lines (189 loc) · 7.24 KB
/
Copy pathScript.js
File metadata and controls
230 lines (189 loc) · 7.24 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
223
224
225
226
227
228
229
230
//Global variable for changing the current song
let currentSong = new Audio();
let songs;
let currFolder;
//By using the chatgpt - write the function that takes the seconds and convert it into the formate of seconds/minutes
// Function to convert seconds to minutes and seconds format
function secondsToMinutesSeconds(seconds) {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = Math.floor(seconds % 60);
// Add leading zeros if necessary
const formattedMinutes = String(minutes).padStart(2, "0");
const formattedSeconds = String(remainingSeconds).padStart(2, "0");
return `${formattedMinutes}:${formattedSeconds}`;
}
//The getSongs function do's the work of fetching the songs from the local directory and return the songs.
async function getSongs(folder) {
currFolder = folder;
let a = await fetch(`http://127.0.0.1:5500/${folder}/`);
let response = await a.text();
let div = document.createElement("div");
div.innerHTML = response;
let as = div.getElementsByTagName("a");
//Creating the empty array
songs = [];
for (let index = 0; index < as.length; index++) {
const element = as[index];
if (element.href.endsWith(".mp3")) {
songs.push(element.href.split(`${folder}`)[1]);
}
}
//put the songs in the your library in the ul tag & show all the songs in the playlist
let songUL = document
.querySelector(".songList")
.getElementsByTagName("ul")[0];
songUL.innerHTML = " ";
for (const song of songs) {
songUL.innerHTML =
songUL.innerHTML +
` <li><img class="invert" src="music.svg" alt="Music">
<div class="info">
<div>${song.replaceAll("%20", " ")}</div>
<div>Vikrant</div>
</div>
<div class="playnow">
<span>Play Now</span>
<img class="invert" src="play.svg" alt="Play">
</div> </li>`;
}
// Attached the event listener to each song
Array.from(
document.querySelector(".songList").getElementsByTagName("li")
).forEach((e) => {
e.addEventListener("click", (element) => {
console.log(e.querySelector(".info").firstElementChild.innerHTML); //Give me the first element of the info div
playMusic(e.querySelector(".info").firstElementChild.innerHTML.trim()); //Function to play the song & .trim() to remove the spaces from the song for the playMusic function
});
});
}
//creating the function to play the song
const playMusic = (track, pause = false) => {
//let audio = new Audio("/songs/" + track)
//here we do not making the new audio ,but we changing the current source and then we play the audio
currentSong.src = `${currFolder}` + track;
if (!pause) {
currentSong.play();
play.src = "pause.svg"; //firstly it is paused when the song is playing
}
//to display the song info and song time
document.querySelector(".songInfo").innerHTML = decodeURI(track);
document.querySelector(".songTime").innerHTML = "00:00/00:00";
};
//creating the async function to display the albums
async function displayAlbums() {
let a = await fetch(`http://127.0.0.1:5500/songs/`);
let response = await a.text();
let div = document.createElement("div");
div.innerHTML = response;
let anchors = div.getElementsByTagName("a")
let cardContainer = document.querySelector(".cardContainer")
Array.from(anchors).forEach(async e=>{
if(e.href.includes("/songs")){
let folder = e.href.split("/").slice(-1)[0]
//Get the metadata of the folder
let a = await fetch(`http://127.0.0.1:5500/songs/${folder}/info.json`);
let response = await a.json();
console.log(response);
cardContainer.innerHTML = cardContainer.innerHTML +
` <div data-folder="cs" class="card">
<!-- Adding the play button inside the card -->
<div class="play">
<svg
xmlns="http://www.w3.org/2000/svg"
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
color="#000000"
>
<path
d="M5 20V4L19 12L5 20Z"
stroke="#000000"
stroke-width="1.5"
stroke-linejoin="round"
fill="#000"
></path>
</svg>
</div>
<img src="/songs/${folder}/cover.jpg" alt="" />
<h2>${response.title}</h2>
<p>${response.description}</p>
</div>`
}
})
}
//here the promise is pending so for that we can do this and get the list of all songs
async function main() {
//get the list of the song
await getSongs("songs/ncs");
playMusic(songs[0], true);
//Display all the albums on the page
displayAlbums();
//Attached an event listener to play , next and previous
play.addEventListener("click", () => {
if (currentSong.paused) {
currentSong.play();
//Adding the SVG's to play and pause
play.src = "pause.svg";
} else {
currentSong.pause();
play.src = "play.svg";
}
});
//Time update event
currentSong.addEventListener("timeupdate", () => {
console.log(currentSong.currentTime, currentSong.duration);
document.querySelector(".songTime").innerHTML = `${secondsToMinutesSeconds(
currentSong.currentTime
)} / ${secondsToMinutesSeconds(currentSong.duration)}`;
document.querySelector(".circle").style.left =
(currentSong.currentTime / currentSong.duration) * 100 + "%";
//Adding the event listener to seek bar
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 eventlistner to hamburger
document.querySelector(".hambburger").addEventListener("click", () => {
document.querySelector(".left").style.left = 0;
});
//Add eventlistner to close the hamburger
document.querySelector(".close").addEventListener("click", () => {
document.querySelector(".left").style.left = "-120%";
});
//Add an event listener for the previous
previous.addEventListener("click", () => {
console.log("previous Clicked");
console.log(currentSong);
let index = songs.indexOf(currentSong.src.split("/").slice(-1)[0]);
if (index - 1 >= 0) {
playMusic(songs[index - 1]);
}
});
//Add an event listener for the next
next.addEventListener("click", () => {
console.log("Next Clicked");
let index = songs.indexOf(currentSong.src.split("/").slice(-1)[0]);
if (index + 1 < songs.length) {
playMusic(songs[index + 1]);
}
});
//Add an event to volume
document
.querySelector(".range")
.getElementsByTagName("input")[0]
.addEventListener("change", (e) => {
console.log("setting volume to", e.target.value, "/100");
currentSong.volume = parseInt(e.target.value) / 100;
});
//Load the playlist whenever card is clicked
Array.from(document.getElementsByClassName("card")).forEach((e) => {
console.log(e);
e.addEventListener("click", async (item) => {
songs = await getSongs(`songs/${item.currentTarget.dataset.folder}`);
});
});
}
main();