-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
410 lines (300 loc) · 9.44 KB
/
script.js
File metadata and controls
410 lines (300 loc) · 9.44 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
'use strict';
/**
* all music information
*/
const musicData = [
{
backgroundImage: "./poster-1.jpg",
posterUrl: "./poster-1.jpg",
title: "Enthan Nanbiye Nanbiyeee",
album: "TEDDY",
year: 2020,
artist: "Anirudh Ravichanderdas",
musicPath: "./music-1.mp3",
},
{
backgroundImage: "./poster-2.jpg",
posterUrl: "./poster-2.jpg",
title: " Enna Panni Tholachaaa En Nenjukulla Rattinangal Suthuthadiii",
album: "MUTHUKU MUTHAAGA",
year: 2011,
artist: "Vijay Yesudas ",
musicPath: "./music-2.mp3",
},
{
backgroundImage: "./poster-3.jpg",
posterUrl: "./poster-3.jpg",
title: "Ennai vittu uyir ponalum unnai vittu naan po matten",
album: "LOVE TODAY",
year: 2022,
artist: "Yuvansankar Raja",
musicPath: "./music-3.mp3",
},
{
backgroundImage: "./poster-4.jpg",
posterUrl: "./poster-4.jpg",
title: "Amutha kadal unakutha aara malai unaku thaa",
album: "CHITHTHA",
year: 2023,
artist: "Santhosh Narayanan",
musicPath: "./music-4.mp3",
},
{
backgroundImage: "./poster-5.jpg",
posterUrl: "./poster-5.jpg",
title: "Usuree nithanee nee thanee",
album: "RAAYAN",
year: 2024,
artist: "A.R.Rahman",
musicPath: "./music-5.mp3",
},
];
/**
* add eventListnere on all elements that are passed
*/
const addEventOnElements = function (elements, eventType, callback) {
for (let i = 0, len = elements.length; i < len; i++) {
elements[i].addEventListener(eventType, callback);
}
}
/**
* PLAYLIST
*
* add all music in playlist, from 'musicData'
*/
const playlist = document.querySelector("[data-music-list]");
for (let i = 0, len = musicData.length; i < len; i++) {
playlist.innerHTML += `
<li>
<button class="music-item ${i === 0 ? "playing" : ""}" data-playlist-toggler data-playlist-item="${i}">
<img src="${musicData[i].posterUrl}" width="800" height="800" alt="${musicData[i].title} Album Poster"
class="img-cover">
<div class="item-icon">
<span class="material-symbols-rounded">equalizer</span>
</div>
</button>
</li>
`;
}
/**
* PLAYLIST MODAL SIDEBAR TOGGLE
*
* show 'playlist' modal sidebar when click on playlist button in top app bar
* and hide when click on overlay or any playlist-item
*/
const playlistSideModal = document.querySelector("[data-playlist]");
const playlistTogglers = document.querySelectorAll("[data-playlist-toggler]");
const overlay = document.querySelector("[data-overlay]");
const togglePlaylist = function () {
playlistSideModal.classList.toggle("active");
overlay.classList.toggle("active");
document.body.classList.toggle("modalActive");
}
addEventOnElements(playlistTogglers, "click", togglePlaylist);
/**
* PLAYLIST ITEM
*
* remove active state from last time played music
* and add active state in clicked music
*/
const playlistItems = document.querySelectorAll("[data-playlist-item]");
let currentMusic = 0;
let lastPlayedMusic = 0;
const changePlaylistItem = function () {
playlistItems[lastPlayedMusic].classList.remove("playing");
playlistItems[currentMusic].classList.add("playing");
}
addEventOnElements(playlistItems, "click", function () {
lastPlayedMusic = currentMusic;
currentMusic = Number(this.dataset.playlistItem);
changePlaylistItem();
});
/**
* PLAYER
*
* change all visual information on player, based on current music
*/
const playerBanner = document.querySelector("[data-player-banner]");
const playerTitle = document.querySelector("[data-title]");
const playerAlbum = document.querySelector("[data-album]");
const playerYear = document.querySelector("[data-year]");
const playerArtist = document.querySelector("[data-artist]");
const audioSource = new Audio(musicData[currentMusic].musicPath);
const changePlayerInfo = function () {
playerBanner.src = musicData[currentMusic].posterUrl;
playerBanner.setAttribute("alt", `${musicData[currentMusic].title} Album Poster`);
document.body.style.backgroundImage = `url(${musicData[currentMusic].backgroundImage})`;
playerTitle.textContent = musicData[currentMusic].title;
playerAlbum.textContent = musicData[currentMusic].album;
playerYear.textContent = musicData[currentMusic].year;
playerArtist.textContent = musicData[currentMusic].artist;
audioSource.src = musicData[currentMusic].musicPath;
audioSource.addEventListener("loadeddata", updateDuration);
playMusic();
}
addEventOnElements(playlistItems, "click", changePlayerInfo);
/** update player duration */
const playerDuration = document.querySelector("[data-duration]");
const playerSeekRange = document.querySelector("[data-seek]");
/** pass seconds and get timcode formate */
const getTimecode = function (duration) {
const minutes = Math.floor(duration / 60);
const seconds = Math.ceil(duration - (minutes * 60));
const timecode = `${minutes}:${seconds < 10 ? "0" : ""}${seconds}`;
return timecode;
}
const updateDuration = function () {
playerSeekRange.max = Math.ceil(audioSource.duration);
playerDuration.textContent = getTimecode(Number(playerSeekRange.max));
}
audioSource.addEventListener("loadeddata", updateDuration);
/**
* PLAY MUSIC
*
* play and pause music when click on play button
*/
const playBtn = document.querySelector("[data-play-btn]");
let playInterval;
const playMusic = function () {
if (audioSource.paused) {
audioSource.play();
playBtn.classList.add("active");
playInterval = setInterval(updateRunningTime, 500);
} else {
audioSource.pause();
playBtn.classList.remove("active");
clearInterval(playInterval);
}
}
playBtn.addEventListener("click", playMusic);
/** update running time while playing music */
const playerRunningTime = document.querySelector("[data-running-time");
const updateRunningTime = function () {
playerSeekRange.value = audioSource.currentTime;
playerRunningTime.textContent = getTimecode(audioSource.currentTime);
updateRangeFill();
isMusicEnd();
}
/**
* RANGE FILL WIDTH
*
* change 'rangeFill' width, while changing range value
*/
const ranges = document.querySelectorAll("[data-range]");
const rangeFill = document.querySelector("[data-range-fill]");
const updateRangeFill = function () {
let element = this || ranges[0];
const rangeValue = (element.value / element.max) * 100;
element.nextElementSibling.style.width = `${rangeValue}%`;
}
addEventOnElements(ranges, "input", updateRangeFill);
/**
* SEEK MUSIC
*
* seek music while changing player seek range
*/
const seek = function () {
audioSource.currentTime = playerSeekRange.value;
playerRunningTime.textContent = getTimecode(playerSeekRange.value);
}
playerSeekRange.addEventListener("input", seek);
/**
* END MUSIC
*/
const isMusicEnd = function () {
if (audioSource.ended) {
playBtn.classList.remove("active");
audioSource.currentTime = 0;
playerSeekRange.value = audioSource.currentTime;
playerRunningTime.textContent = getTimecode(audioSource.currentTime);
updateRangeFill();
}
}
/**
* SKIP TO NEXT MUSIC
*/
const playerSkipNextBtn = document.querySelector("[data-skip-next]");
const skipNext = function () {
lastPlayedMusic = currentMusic;
if (isShuffled) {
shuffleMusic();
} else {
currentMusic >= musicData.length - 1 ? currentMusic = 0 : currentMusic++;
}
changePlayerInfo();
changePlaylistItem();
}
playerSkipNextBtn.addEventListener("click", skipNext);
/**
* SKIP TO PREVIOUS MUSIC
*/
const playerSkipPrevBtn = document.querySelector("[data-skip-prev]");
const skipPrev = function () {
lastPlayedMusic = currentMusic;
if (isShuffled) {
shuffleMusic();
} else {
currentMusic <= 0 ? currentMusic = musicData.length - 1 : currentMusic--;
}
changePlayerInfo();
changePlaylistItem();
}
playerSkipPrevBtn.addEventListener("click", skipPrev);
/**
* SHUFFLE MUSIC
*/
/** get random number for shuffle */
const getRandomMusic = () => Math.floor(Math.random() * musicData.length);
const shuffleMusic = () => currentMusic = getRandomMusic();
const playerShuffleBtn = document.querySelector("[data-shuffle]");
let isShuffled = false;
const shuffle = function () {
playerShuffleBtn.classList.toggle("active");
isShuffled = isShuffled ? false : true;
}
playerShuffleBtn.addEventListener("click", shuffle);
/**
* REPEAT MUSIC
*/
const playerRepeatBtn = document.querySelector("[data-repeat]");
const repeat = function () {
if (!audioSource.loop) {
audioSource.loop = true;
this.classList.add("active");
} else {
audioSource.loop = false;
this.classList.remove("active");
}
}
playerRepeatBtn.addEventListener("click", repeat);
/**
* MUSIC VOLUME
*
* increase or decrease music volume when change the volume range
*/
const playerVolumeRange = document.querySelector("[data-volume]");
const playerVolumeBtn = document.querySelector("[data-volume-btn]");
const changeVolume = function () {
audioSource.volume = playerVolumeRange.value;
audioSource.muted = false;
if (audioSource.volume <= 0.1) {
playerVolumeBtn.children[0].textContent = "volume_mute";
} else if (audioSource.volume <= 0.5) {
playerVolumeBtn.children[0].textContent = "volume_down";
} else {
playerVolumeBtn.children[0].textContent = "volume_up";
}
}
playerVolumeRange.addEventListener("input", changeVolume);
/**
* MUTE MUSIC
*/
const muteVolume = function () {
if (!audioSource.muted) {
audioSource.muted = true;
playerVolumeBtn.children[0].textContent = "volume_off";
} else {
changeVolume();
}
}
playerVolumeBtn.addEventListener("click", muteVolume);