-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
253 lines (235 loc) · 7.22 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
console.log("Welcome to Spotify");
let songIndex = 0;
let audioElement = new Audio("songs/1.mp3");
let masterPlay = document.getElementById("masterPlay");
let myProgressBar = document.getElementById("myProgressBar");
let gif = document.getElementById("gif");
let songItem = Array.from(document.getElementsByClassName("songItem"));
let masterSongName = document.getElementById("masterSongName");
let uploadSong = document.getElementById("uploadSong");
let uploadImage = document.getElementById("uploadImage");
let songNameInput = document.getElementById("songNameInput");
let addSongButton = document.getElementById("addSongButton");
let searchInput = document.getElementById("searchInput");
let searchButton = document.getElementById("searchButton");
let songs = [
{
songName: "Blinding Lights",
filePath: "./songs/1.mp3",
coverPath: "./songIMG/1.jpg",
},
{
songName: "FE!N",
filePath: "songs/2.mp3",
coverPath: "./songIMG/2.jpg",
},
{
songName: "Kun Faya Kun",
filePath: "songs/3.mp3",
coverPath: "./songIMG/3.jpg",
},
{
songName: "Night Changes",
filePath: "songs/4.mp3",
coverPath: "./songIMG/4.jpg",
},
{
songName: "Until I found you",
filePath: "songs/5.mp3",
coverPath: "./songIMG/5.jpg",
},
{
songName: "Perfect",
filePath: "songs/2.mp3",
coverPath: "./songIMG/6.jpg",
},
{
songName: "Shut up and dance",
filePath: "songs/2.mp3",
coverPath: "./songIMG/7.jpg",
},
{
songName: "Into the Night",
filePath: "songs/2.mp3",
coverPath: "./songIMG/8.jpg",
},
{
songName: "Cake by the ocean",
filePath: "songs/2.mp3",
coverPath: "./songIMG/9.jpg",
},
{
songName: "Gimme Love",
filePath: "songs/4.mp3",
coverPath: "./songIMG/10.jpg",
},
{
songName: "Sawar Loon",
filePath: "songs/4.mp3",
coverPath: "./songIMG/11.jpg",
},
{
songName: "Jashn-E-Bahara",
filePath: "songs/4.mp3",
coverPath: "./songIMG/12.jpg",
},
{
songName: "Pal Pal dilke pass",
filePath: "songs/4.mp3",
coverPath: "./songIMG/13.jpg",
},
{
songName: "All the stars",
filePath: "songs/4.mp3",
coverPath: "./songIMG/14.jpg",
},
{
songName: "The Night we met",
filePath: "songs/4.mp3",
coverPath: "./songIMG/15.jpg",
},
{
songName: "Mast Magan",
filePath: "songs/4.mp3",
coverPath: "./songIMG/16.jpg",
},
];
songItem.forEach((element, i) => {
element.getElementsByTagName("img")[0].src = songs[i].coverPath;
element.getElementsByClassName("songName")[0].innerText = songs[i].songName;
});
masterPlay.addEventListener("click", () => {
if (audioElement.paused || audioElement.currentTime <= 0) {
audioElement.play();
masterPlay.classList.remove("fa-play-circle");
masterPlay.classList.add("fa-pause-circle");
gif.style.opacity = 1;
} else {
audioElement.pause();
masterPlay.classList.remove("fa-pause-circle");
masterPlay.classList.add("fa-play-circle");
gif.style.opacity = 0;
}
});
audioElement.addEventListener("timeupdate", () => {
let prog = parseInt((audioElement.currentTime / audioElement.duration) * 100);
myProgressBar.value = prog;
});
myProgressBar.addEventListener("change", () => {
audioElement.currentTime =
(myProgressBar.value * audioElement.duration) / 100;
});
const makeAllPlays = () => {
Array.from(document.getElementsByClassName("songItemPlay")).forEach(
(element) => {
element.classList.remove("fa-pause-circle");
element.classList.add("fa-play-circle");
}
);
};
Array.from(document.getElementsByClassName("songItemPlay")).forEach(
(element) => {
element.addEventListener("click", (e) => {
makeAllPlays();
songIndex = parseInt(e.target.id);
e.target.classList.remove("fa-play-circle");
e.target.classList.add("fa-pause-circle");
audioElement.src = `songs/${songIndex + 1}.mp3`;
masterSongName.innerText = songs[songIndex].songName;
audioElement.currentTime = 0;
audioElement.play();
gif.style.opacity = 1;
masterPlay.classList.remove("fa-play-circle");
masterPlay.classList.add("fa-pause-circle");
});
}
);
document.getElementById("next").addEventListener("click", () => {
if (songIndex >= songs.length - 1) {
songIndex = 0;
} else {
songIndex += 1;
}
audioElement.src = `songs/${songIndex + 1}.mp3`;
masterSongName.innerText = songs[songIndex].songName;
audioElement.currentTime = 0;
audioElement.play();
masterPlay.classList.remove("fa-play-circle");
masterPlay.classList.add("fa-pause-circle");
});
document.getElementById("previous").addEventListener("click", () => {
if (songIndex <= 0) {
songIndex = 0;
} else {
songIndex -= 1;
}
audioElement.src = `songs/${songIndex + 1}.mp3`;
masterSongName.innerText = songs[songIndex].songName;
audioElement.currentTime = 0;
audioElement.play();
masterPlay.classList.remove("fa-play-circle");
masterPlay.classList.add("fa-pause-circle");
});
searchButton.addEventListener("click", () => {
let query = searchInput.value.toLowerCase();
let filteredSongs = songs.filter((song) =>
song.songName.toLowerCase().includes(query)
);
if (filteredSongs.length > 0) {
songItem.forEach((element, i) => {
if (i < filteredSongs.length) {
element.style.display = "flex";
element.getElementsByTagName("img")[0].src = filteredSongs[i].coverPath;
element.getElementsByClassName("songName")[0].innerText =
filteredSongs[i].songName;
element.getElementsByClassName("songItemPlay")[0].id = songs.indexOf(
filteredSongs[i]
);
} else {
element.style.display = "none";
}
});
} else {
songItem.forEach((element) => {
element.style.display = "none";
});
}
});
addSongButton.addEventListener("click", () => {
const songFile = uploadSong.files[0];
const imageFile = uploadImage.files[0];
const songName = songNameInput.value;
if (songFile && songName) {
const songUrl = URL.createObjectURL(songFile);
const imageUrl = imageFile ? URL.createObjectURL(imageFile) : "./songIMG/default.jpg";
const newSong = {
songName: songName,
filePath: songUrl,
coverPath: imageUrl,
};
songs.push(newSong);
const newElement = document.createElement("div");
newElement.classList.add("songItem");
newElement.innerHTML = `
<img src="${newSong.coverPath}" alt="cover">
<span class="songName">${newSong.songName}</span>
<span class="songlistplay">
<span class="timestamp">00:00 <i class="far songItemPlay fa-play-circle"></i> </span>
</span>`;
document.querySelector(".songItemContainer").appendChild(newElement);
songItem.push(newElement);
newElement.querySelector(".songItemPlay").addEventListener("click", (e) => {
makeAllPlays();
songIndex = songs.length - 1;
e.target.classList.remove("fa-play-circle");
e.target.classList.add("fa-pause-circle");
audioElement.src = newSong.filePath;
masterSongName.innerText = newSong.songName;
audioElement.currentTime = 0;
audioElement.play();
gif.style.opacity = 1;
masterPlay.classList.remove("fa-play-circle");
masterPlay.classList.add("fa-pause-circle");
});
}
});