-
Notifications
You must be signed in to change notification settings - Fork 5
/
script.js
197 lines (175 loc) · 5.7 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
document.addEventListener('DOMContentLoaded', function () {
const tracksList = [
{
audioSrc:
'./tracks/01-Wiz Khalifa - Amber Ice [Prod. By I.D. Labs Productions] [www.SongsLover.com].mp3',
coverSrc: './images/wizKhalifa.jpg',
name: 'Wiz Khalifa',
desc: 'Wiz Khalifa - Amber Ice',
id: 0,
},
{
audioSrc:
'./tracks/Martin Garrix & Tiësto - The Only Way Is Up (Official Music Video).mp3',
coverSrc: './images/martinG.jpg',
name: 'Martin Garrix',
desc: 'Ft. Tiesto Only Way Is Up',
id: 1,
},
{
audioSrc: './tracks/Space Buddha - Self Therapy.mp3',
coverSrc: './images/spaceBuddha.jpg',
name: 'Space Buddha',
desc: 'Self Therapy',
id: 2,
},
{
audioSrc: "./tracks/Drake - God's Plan.mp3",
coverSrc: './images/godsplan.jpg',
name: 'Drake',
desc: "God's plan",
id: 3,
},
{
audioSrc: './tracks/Joyner Lucas & Lil Baby - Ramen & OJ.mp3',
coverSrc: './images/ramenlucas.png',
name: 'Joyner Lucas & Lil Baby',
desc: 'Ramen & OJ',
id: 4,
},
{
audioSrc:
'./tracks/Pop Smoke - For The Night (Audio) ft. Lil Baby, DaBaby.mp3',
coverSrc: './images/popsmoke.jpg',
name: 'Pop Smoke ft. Lil Baby, DaBaby',
desc: 'For The Night',
id: 5,
},
];
const currentTrackName = document.querySelector('header h3');
const currentTrackDesc = document.querySelector('header p');
const currentTrackCover = document.querySelector('header img');
const currentTrackAudio = document.querySelector('audio');
const playPauseBtn = document.querySelector('.event-playPause');
const muteUnmuteBtn = document.querySelector('.event-muteUnmute');
const playNextBtn = document.querySelector('.event-next');
const playPrevBtn = document.querySelector('.event-prev');
const progress = document.querySelector('.slider-progress');
const currentTrackTime = document.querySelector('.count-current');
const finalTrackTime = document.querySelector('.count-final');
// ADD TRACKS TO MY PLAYLIST ON PAGE LOAD
(function addMyTracksList() {
for (let track of tracksList) {
var li = document.createElement('li');
li.id = track.id;
li.innerHTML = `
<div class="track-number">0${track.id}</div>
<img
src=${track.coverSrc}
class="track-img"
alt=""
/>
<div class="track-detail">
<div class="track-detail_name">${track.name}</div>
<div class="track-detail_desc">
<small>${track.desc}</small>
</div>
</div>
`;
document.querySelector('ul').appendChild(li);
(function (id) {
li.addEventListener(
'click',
() => {
playSelectedTrack(id);
},
false
);
})(track.id);
}
})();
let trackId = 0;
const loadTrack = (songId) => {
const song = tracksList.find((track) => track.id === songId);
const { audioSrc, coverSrc, name, desc } = song;
currentTrackName.innerText = name;
currentTrackDesc.innerText = desc;
currentTrackAudio.src = audioSrc;
currentTrackCover.src = coverSrc;
};
const playSelectedTrack = (songId) => {
trackId = songId;
loadTrack(songId);
playTrack();
};
loadTrack(trackId);
const playTrack = () => {
playPauseBtn.classList.remove('fa-play');
playPauseBtn.classList.add('fa-pause');
currentTrackAudio.play();
};
const pauseTrack = () => {
playPauseBtn.classList.remove('fa-pause');
playPauseBtn.classList.add('fa-play');
currentTrackAudio.pause();
};
const playPrevTrack = () => {
trackId--;
if (trackId < 0) {
trackId = tracksList.length - 1;
}
loadTrack(trackId);
playTrack();
};
const playNextTrack = () => {
trackId++;
if (trackId > tracksList.length - 1) {
trackId = 0;
}
loadTrack(trackId);
playTrack();
};
const updateProgress = () => {
const currentTime = currentTrackAudio.currentTime;
const trackDuration = currentTrackAudio.duration;
const percent = (currentTime / trackDuration) * 100;
progress.style.width = percent + '%';
let curMins = Math.floor(currentTime / 60);
let curSecs = Math.floor(currentTime - curMins * 60);
let durMins = Math.floor(trackDuration / 60) || '--';
let durSecs = Math.floor(trackDuration - durMins * 60) || '--';
if (curMins < 10) {
curMins = `0${curMins}`;
}
if (curSecs < 10) {
curSecs = `0${curSecs}`;
}
if (durMins < 10) {
durMins = `0${durMins}`;
}
if (durSecs < 10) {
durSecs = `0${durSecs}`;
}
currentTrackTime.innerText = `${curMins}:${curSecs}`;
finalTrackTime.innerText = `${durMins}:${durSecs}`;
};
const muteUnmuteTrack = () => {
if (currentTrackAudio.muted) {
currentTrackAudio.muted = false;
muteUnmuteBtn.classList.remove('fa-volume-mute');
muteUnmuteBtn.classList.add('fa-volume-up');
} else {
currentTrackAudio.muted = true;
muteUnmuteBtn.classList.remove('fa-volume-up');
muteUnmuteBtn.classList.add('fa-volume-mute');
}
};
playPauseBtn.addEventListener('click', () => {
const currentlyPlaying = playPauseBtn.classList.contains('fa-pause');
currentlyPlaying ? pauseTrack() : playTrack();
});
muteUnmuteBtn.addEventListener('click', () => muteUnmuteTrack());
playPrevBtn.addEventListener('click', () => playPrevTrack());
playNextBtn.addEventListener('click', () => playNextTrack());
currentTrackAudio.addEventListener('timeupdate', () => updateProgress());
});