-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
250 lines (206 loc) · 8.21 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
let track_index = 0;
let isPlaying = false;
let updateTimer;
let shuffle = false;
let track_name = document.querySelector("#track-name");
let track_artist = document.querySelector("#track-artist");
let track_art = document.querySelector("#track-art");
let playpause_btn = document.querySelector("#play_pause");
let previous_btn = document.querySelector("#previous-track");
let next_btn = document.querySelector("#next-track");
let shuffle_btn = document.querySelector("#shuffle-track");
let seek_slider = document.querySelector(".seek_slider");
let volume_slider = document.querySelector(".volume_slider");
let curr_time = document.querySelector(".current-time");
let total_duration = document.querySelector(".total-duration");
// Create the audio element for the player
let curr_track = document.createElement('audio');
let track_list = [
{
name: "Muerto en Choroní",
artist: "Circo Urbano",
album_image_small_jpg: "circo_urbano_kilometros_cover.webp",
album_image_small_webp: "circo_urbano_kilometros_cover.webp",
album_image_big_jpg: "fondo_metal_lijado_remache.jpg",
album_image_big_webp: "fondo_metal_lijado_remache.jpg",
path: "muerto-en-choroni_circo_urbano.mp3"
},
{
name: "Arenita Playita",
artist: "Cuarto Poder",
album_image_small_jpg: "4to_poder_cover.jpg",
album_image_small_webp: "4to_poder_cover.jpg",
album_image_big_jpg: "metálico_estriado.jpg",
album_image_big_webp: "metálico_estriado.jpg",
path: "arenita_playita_4to_poder.mp3"
},
{
name: "Jah Jah Go",
artist: "Mulato",
album_image_small_jpg: "mulato_cover.jpg",
album_image_small_webp: "mulato_cover.jpg",
album_image_big_jpg: "black_honeycomb.jpg",
album_image_big_webp: "black_honeycomb.jpg",
path: "jah_jah_go_mulato.mp3"
},
{
name: "La Casa",
artist: "Caramelos de Cianuro",
album_image_small_jpg: "caramelos_de_cianuro_album.png",
album_image_small_webp: "caramelos_de_cianuro_album.png",
album_image_big_jpg: "metal_lijado.jpg",
album_image_big_webp: "metal_lijado.jpg",
path: "la_casa_caramelos_de_cianuro.mp3"
},
{
name: "Uñas Asesinas",
artist: "Zapato 3",
album_image_small_jpg: "unas_asesinas_zapato_3.jpg",
album_image_small_webp: "unas_asesinas_zapato_3.jpg",
album_image_big_jpg: "rust_metal_rivets.jpg",
album_image_big_webp: "rust_metal_rivets.jpg",
path: "unas_asesinas_zapato_3.mp3"
}
];
shuffle_btn.addEventListener("click", () => {
if (shuffle === false) {
shuffle = true;
document.querySelector(".fa-random").className += " active";
}
else {
shuffle = false;
removeActive()
track_index = track_list.findIndex(x => x.path === curr_track.getAttribute("src"));
}
})
playpause_btn.addEventListener("click", () => {
playpauseTrack();
})
previous_btn.addEventListener("click", () => {
prevTrack();
})
next_btn.addEventListener("click", () => {
nextTrack();
})
function removeActive() {
var current = document.querySelectorAll(".active");
current[0].className = current[0].className.replace(" active", "");
}
function playpauseTrack() {
if (!isPlaying)
playTrack();
else
pauseTrack();
}
function playTrack() {
curr_track.play();
isPlaying = true;
playpause_btn.innerHTML = '<i class="fas fa-pause"></i>';
}
function randomIndex(arr, excludeIndex) {
let indexes = Object.keys(arr); //get a list of indexes
indexes.splice(excludeIndex, 1); //remove the unwanted
return indexes[Math.floor(Math.random() * indexes.length)]; //pick a new index
}
function loadTrack(track_index) {
// Clear the previous seek timer
clearInterval(updateTimer);
resetValues();
// If shuffle is activated choose a random song without repeating
if (shuffle) {
track_index = randomIndex(track_list, track_list.findIndex(x => x.path === curr_track.getAttribute("src")));
}
// Load a new track
curr_track.src = track_list[track_index].path;
curr_track.load();
// Update details of the track
track_art.style.backgroundImage = `linear-gradient(to bottom, transparent 0%, black), url(${track_list[track_index].album_image_small_webp})`;
track_name.textContent = track_list[track_index].name;
track_artist.textContent = track_list[track_index].artist;
track_name.classList.add('animate__animated', 'animate__fadeInUp');
track_artist.classList.add('animate__animated', 'animate__fadeInUp');
track_name.addEventListener('animationend', () => {
track_name.classList.remove('animate__animated', 'animate__fadeInUp');
});
track_artist.addEventListener('animationend', () => {
track_artist.classList.remove('animate__animated', 'animate__fadeInUp');
});
document.body.style.background = "#000000";
document.body.style.background = `-webkit-radial-gradient(center, rgba(0, 0, 0, 0.4), #000000 90%), url(${track_list[track_index].album_image_big_webp})`;
document.body.style.background = `-moz-radial-gradient(center, rgba(0, 0, 0, 0.4), #000000 90%), url(${track_list[track_index].album_image_big_webp})`;
document.body.style.background = `radial-gradient(ellipse at center, rgba(0, 0, 0, 0.4), #000000 90%), url(${track_list[track_index].album_image_big_webp})`;
// Set an interval of 1000 milliseconds
// for updating the seek slider
updateTimer = setInterval(seekUpdate, 1000);
// Move to the next track if the current finishes playing
// using the 'ended' event
curr_track.addEventListener("ended", nextTrack);
}
function pauseTrack() {
curr_track.pause();
isPlaying = false;
playpause_btn.innerHTML = '<i class="fa fa-play"></i>';
}
function resetValues() {
curr_time.textContent = "00:00";
total_duration.textContent = "00:00";
seek_slider.value = 0;
}
function prevTrack() {
if (track_index > 0)
track_index -= 1;
else track_index = track_list.length - 1;
loadTrack(track_index);
playTrack();
}
function nextTrack() {
if (track_index < track_list.length - 1)
track_index += 1;
else
track_index = 0;
loadTrack(track_index);
playTrack();
}
function seekTo() {
seekto = curr_track.duration * (seek_slider.value / 100);
curr_track.currentTime = seekto;
}
function setVolume() {
curr_track.volume = volume_slider.value / 100;
}
function seekUpdate() {
let seekPosition = 0;
// Check if the current track duration is a legible number
if (!isNaN(curr_track.duration)) {
seekPosition = curr_track.currentTime * (100 / curr_track.duration);
seek_slider.value = seekPosition;
// Calculate the time left and the total duration
let currentMinutes = Math.floor(curr_track.currentTime / 60);
let currentSeconds = Math.floor(curr_track.currentTime - currentMinutes * 60);
let durationMinutes = Math.floor(curr_track.duration / 60);
let durationSeconds = Math.floor(curr_track.duration - durationMinutes * 60);
// Add a zero to the single digit time values
if (currentSeconds < 10) { currentSeconds = "0" + currentSeconds; }
if (durationSeconds < 10) { durationSeconds = "0" + durationSeconds; }
if (currentMinutes < 10) { currentMinutes = "0" + currentMinutes; }
if (durationMinutes < 10) { durationMinutes = "0" + durationMinutes; }
// Display the updated duration
curr_time.textContent = currentMinutes + ":" + currentSeconds;
total_duration.textContent = durationMinutes + ":" + durationSeconds;
}
}
document.addEventListener("click", e => {
const isDropdownButton = e.target.matches("[data-dropdown-button]")
if (!isDropdownButton && e.target.closest("[data-dropdown]") != null) return
let currentDropdown
if (isDropdownButton) {
currentDropdown = e.target.closest("[data-dropdown]")
currentDropdown.classList.toggle("active")
}
document.querySelectorAll("[data-dropdown].active").forEach(dropdown => {
if (dropdown === currentDropdown) return
dropdown.classList.remove("active")
})
})
// Load the first track in the tracklist
loadTrack(track_index);