-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
227 lines (207 loc) · 5.74 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
const image = document.querySelector('img');
const title = document.getElementById('title');
const artist = document.getElementById('artist');
const music = document.querySelector('audio');
const currentTimeEl = document.getElementById('current-time');
const durationEl = document.getElementById('duration');
const progress = document.getElementById('progress');
const progressContainer = document.getElementById('progress-container');
const prevBtn = document.getElementById('prev');
const playBtn = document.getElementById('play');
const nextBtn = document.getElementById('next');
const volumeIcon = document.getElementById('volume-icon');
const volumeRange = document.querySelector('.volume-range');
const volumeBar = document.querySelector('.volume-bar');
// Music
const songs = [
{
name: 'Liz-1',
displayName: 'Erase',
artist: 'FRND',
},
{
name: 'Liz-2',
displayName: 'Reflejo',
artist: 'Charly Flow',
},
{
name: 'Liz-3',
displayName: 'Dimple',
artist: 'BTS',
},
{
name: 'Liz-4',
displayName: 'What If I Fall Apart',
artist: 'FRND',
},
{
name: 'Liz-5',
displayName: 'On Your Collarbone',
artist: 'Jordan Klassen',
},
{
name: 'Liz-6',
displayName: 'Sin Ti',
artist: 'Sebastian Yatra',
},
{
name: 'Liz-7',
displayName: 'Siren (Remix)',
artist: '(UNEDUCATED KID, Paul Blanco)',
},
{
name: 'Liz-8',
displayName: 'There x2',
artist: 'Slushii (Feat: Marshmellow)',
},
{
name: 'Liz-9',
displayName: 'Take It All Back',
artist: 'Judah & The Lion',
},
{
name: 'Liz-10',
displayName: 'To Zion',
artist: 'Trevor Hall',
},
];
// Check if Playing
let isPlaying = false;
// Play
function playSong() {
isPlaying = true;
playBtn.classList.replace('fa-play', 'fa-pause');
playBtn.setAttribute('title', 'Pause');
music.play();
}
// Pause
function pauseSong() {
isPlaying = false;
playBtn.classList.replace('fa-pause', 'fa-play');
playBtn.setAttribute('title', 'Play');
music.pause();
}
// Play or Pause Event Listener
playBtn.addEventListener('click', () => (isPlaying ? pauseSong() : playSong()));
// Update DOM
function loadSong(song) {
title.textContent = song.displayName;
artist.textContent = song.artist;
music.src = `music/${song.name}.mp3`;
image.src = `img/${song.name}.jpg`;
}
// Current Song
let songIndex = 0;
// Previous Song
function prevSong() {
songIndex--;
if (songIndex < 0) {
songIndex = songs.length - 1;
}
loadSong(songs[songIndex]);
playSong();
}
// Next Song
function nextSong() {
songIndex++;
if (songIndex > songs.length - 1) {
songIndex = 0;
}
loadSong(songs[songIndex]);
playSong();
}
// On Load - Select First Song
loadSong(songs[songIndex]);
// Update Progress Bar & Time
function updateProgressBar(e) {
if (isPlaying) {
const { duration, currentTime } = e.srcElement;
// Update progress bar width
const progressPercent = (currentTime / duration) * 100;
progress.style.width = `${progressPercent}%`;
// Calculate display for duration
const durationMinutes = Math.floor(duration / 60);
let durationSeconds = Math.floor(duration % 60);
if (durationSeconds < 10) {
durationSeconds = `0${durationSeconds}`;
}
// Delay switching duration Element to avoid NaN
if (durationSeconds) {
durationEl.textContent = `${durationMinutes}:${durationSeconds}`;
}
// Calculate display for currentTime
const currentMinutes = Math.floor(currentTime / 60);
let currentSeconds = Math.floor(currentTime % 60);
if (currentSeconds < 10) {
currentSeconds = `0${currentSeconds}`;
}
currentTimeEl.textContent = `${currentMinutes}:${currentSeconds}`;
}
}
// Set Progress Bar
function setProgressBar(e) {
const width = this.clientWidth;
const clickX = e.offsetX;
const { duration } = music;
music.currentTime = (clickX / width) * duration;
}
// Volume Controls --------------------------- //
let lastVolume = 1;
// Mute
function toggleMute() {
volumeIcon.className = '';
if (music.volume) {
lastVolume = music.volume;
music.volume = 0;
volumeIcon.classList.add('fas', 'fa-volume-mute');
volumeIcon.setAttribute('title', 'Unmute');
volumeBar.style.width = 0;
} else {
music.volume = lastVolume;
volumeIcon.classList.add('fas', 'fa-volume-up');
volumeIcon.setAttribute('title', 'Mute');
volumeBar.style.width = `${lastVolume * 100}%`;
}
}
// Volume Bar
function changeVolume(e) {
let volume = e.offsetX / volumeRange.offsetWidth;
// Rounding volume up or down
if (volume < 0.1) {
volume = 0;
}
if (volume > 0.9) {
volume = 1;
}
volumeBar.style.width = `${volume * 100}%`;
music.volume = volume;
// Change icon depending on volume
volumeIcon.className = '';
if (volume > 0.7) {
volumeIcon.classList.add('fas', 'fa-volume-up');
} else if (volume < 0.7 && volume > 0) {
volumeIcon.classList.add('fas', 'fa-volume-down');
} else if (volume === 0) {
volumeIcon.classList.add('fas', 'fa-volume-off');
}
lastVolume = volume;
}
// // slider
// function sliderDisplay(){
// volumeBar.style.margin('0px 2px 0px 0px');
// volumeRange.style.width='53px';
// }
// function sliderDisplay(){
// volumeBar.style.margin('0px 0px 0px 0px');
// volumeRange.style.width='0px';
// }
// Event Listeners
prevBtn.addEventListener('click', prevSong);
nextBtn.addEventListener('click', nextSong);
music.addEventListener('ended', nextSong);
music.addEventListener('timeupdate', updateProgressBar);
progressContainer.addEventListener('click', setProgressBar);
volumeRange.addEventListener('click', changeVolume);
volumeIcon.addEventListener('click', toggleMute);
// volumeIcon.addEventListener('mouseover', sliderDisplay);
// volumeIcon.addEventListener('mouseleave', sliderDisplay);