-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
396 lines (361 loc) · 10.9 KB
/
Copy pathapp.js
File metadata and controls
396 lines (361 loc) · 10.9 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
const $ = document.querySelector.bind(document);
const $$ = document.querySelectorAll.bind(document);
// 1. Render songs v
// 2. Scroll v
// 3. Play/ pause / seek(tua) v
// 4. CD rotate(quay) v
// 5. Next / prev v
// 6. Turn on / Turn off randomSong (chưa tối ưu việc random lại quá nhiều 1 bài) v
// 7. NextSong / repeat when ended v
// 8. Active currentSong v
// 9. Scroll active into view v
// 10. Play song when click
const PLAYER_STORAGE_KEY = 'F8_PLAYER';
const currentTime = $('.currentTime');
const durationTime = $('.durationTime');
const heading = $('header h2');
const cdThumb = $('.cd-thumb');
const playList = $('.playlist');
const playBtn = $('.btn-toggle-play');
const cd = $('.cd');
const progress = $('#progress');
const nextBtn = $('.btn-next');
const prevBtn = $('.btn-prev');
const randomBtn = $('.btn-random');
const repeatBtn = $('.btn-repeat');
const volumeIcon = $('.volume-change');
const volumeBar = $('.volume');
const songPlayedList = new Set();
const app = {
currentIndex: 0,
isPlaying: false,
isRandom: false,
isRepeat: false,
isMute: false,
currentVolume: 0.0,
config: JSON.parse(localStorage.getItem(PLAYER_STORAGE_KEY)) || {
currentVolume: 0.5,
isMute: false,
isRepeat: false,
isRandom: false,
currentIndex: 0,
},
setConfig: function (key, value) {
this.config[key] = value;
localStorage.setItem(PLAYER_STORAGE_KEY, JSON.stringify(this.config));
},
songs: [
{
name: 'Jojo Bizarre Adventure',
singer: 'Gogo theme',
path: './music/audio/JoJo-Bizarre-Adventure.mp3',
image: 'https://pbs.twimg.com/profile_images/1055432796137758720/m7-YLJa1.jpg',
},
{
name: 'Waing For You',
singer: 'Mono',
path: './music/audio/Waiting For You.mp3',
image: 'https://pbs.twimg.com/profile_images/1055432796137758720/m7-YLJa1.jpg',
},
{
name: 'Như Những Phút Ban Đầu',
singer: 'Hoài Lâm',
path: './music/audio/nhuphutbandau.mp3',
image:
'https://pbs.twimg.com/profile_images/1055432796137758720/m7-YLJa1.jpg',
},
{
name: '3107(Cover)',
singer: '3107',
path: './music/audio/3107-Cover-Music-30-365.mp3',
image: 'https://pbs.twimg.com/profile_images/1055432796137758720/m7-YLJa1.jpg',
},
{
name: 'Theres No One At All',
singer: 'Sơn Tùng MTP',
path: './music/audio/Theres-No-One-At-All-Son-Tung-M-TP.mp3',
image: 'https://pbs.twimg.com/profile_images/1055432796137758720/m7-YLJa1.jpg',
},
{
name: 'Buông Đôi Tay Nhau Ra',
singer: 'Sơn Tùng MTP',
path: './music/audio/Buong-Doi-Tay-Nhau-Ra-Son-Tung-M-TP.mp3',
image: 'https://pbs.twimg.com/profile_images/1055432796137758720/m7-YLJa1.jpg',
},
{
name: 'We Dont Talk Anymore',
singer: 'Charlie Puth',
path: './music/audio/We Don_t Talk Anymore - Charlie Puth_ Se.mp3',
image: 'https://pbs.twimg.com/profile_images/1055432796137758720/m7-YLJa1.jpg',
},
{
name: 'Anh Đã Quen Với Cô Đơn',
singer: 'Soobin Hoàng Sơn',
path: './music/audio/Anh-Da-Quen-Voi-Co-Don-Soobin-Hoang-Son.mp3',
image: 'https://pbs.twimg.com/profile_images/1055432796137758720/m7-YLJa1.jpg',
},
{
name: 'Lần Cuối',
singer: 'Karik',
path: './music/audio/Lan-Cuoi-Karik.mp3',
image: 'https://pbs.twimg.com/profile_images/1055432796137758720/m7-YLJa1.jpg',
},
{
name: 'Say Do You',
singer: 'Tiên Tiên',
path: './music/audio/Say-You-Do-Tien-Tien.mp3',
image: 'https://pbs.twimg.com/profile_images/1055432796137758720/m7-YLJa1.jpg',
},
{
name: 'Cơn Mưa Rào',
singer: 'JSOL',
path: './music/audio/Con-Mua-Rao-JSOL.mp3',
image: 'https://pbs.twimg.com/profile_images/1055432796137758720/m7-YLJa1.jpg',
},
{
name: 'Hồi ức',
singer: 'Phan Mạnh Quỳnh',
path: './music/audio/Hoi-Uc-Phan-Manh-Quynh.mp3',
image: 'https://pbs.twimg.com/profile_images/1055432796137758720/m7-YLJa1.jpg',
},
],
defineProperties: function () {
Object.defineProperty(this, 'currentSong', {
get: function () {
return this.songs[this.currentIndex];
},
});
},
render: function () {
const htmls = this.songs.map((song, index) => {
return `
<div class="song ${index === this.currentIndex ? 'active' : ''}" data-index="${index}">
<div class="thumb"
style="background-image: url('${song.image}')">
</div>
<div class="body">
<h3 class="title">${song.name}</h3>
<p class="author">${song.singer}</p>
</div>
<div class="option">
<i class="fas fa-ellipsis-h"></i>
</div>
</div>
`;
});
playList.innerHTML = htmls.join('');
},
// Lắng nghe sự kiện khi kéo tren document
handleEvent: function () {
const _this = this;
const cdWidth = cd.offsetWidth;
// Xử lý phóng to/ thu nhỏ cd
document.onscroll = function () {
// console.log(window.scrollY); // Cách 1: Tùy trình duyệt . có một số trình duyệt không hoạt động
// console.log(document.documentElement.scrollTop); // Cách 2
const scroll = window.screenY || document.documentElement.scrollTop;
const newCdWidth = cdWidth - scroll;
cd.style.width = newCdWidth > 0 ? newCdWidth + 'px' : 0;
cd.style.opacity = newCdWidth / cdWidth;
};
// Xử lý khi click play
playBtn.onclick = function () {
if (!_this.isPlaying) audio.play();
else audio.pause();
};
// Khi song được play
audio.onplay = function () {
playBtn.classList.add('playing');
_this.isPlaying = true;
cbThumbAnimation.play();
_this.activeSong(); // intoScrollView đến song đã lưu
if (_this.isMute) {
audio.volume = 0;
volumeBar.Value = 0;
} else {
audio.volume = _this.currentVolume;
volumeBar.value = _this.currentVolume * 100;
}
};
// Khi song bị pause
audio.onpause = function () {
playBtn.classList.remove('playing');
_this.isPlaying = false;
cbThumbAnimation.pause();
};
// Khi tiến độ bài hát thay đổi
audio.ontimeupdate = function () {
// Do khi vừa tải video thì nó là naN
if (audio.duration) {
const progressPercent = Math.floor((audio.currentTime / audio.duration) * 100);
progress.value = progressPercent;
currentTime.textContent = _this.timeFormat(this.currentTime);
durationTime.textContent = _this.timeFormat(this.duration);
}
};
// Xử lý khi tua song
progress.onchange = function (e) {
if (!_this.isPlaying) audio.play();
const seekTime = (audio.duration / 100) * e.target.value;
audio.currentTime = seekTime;
};
// CD rotate
const cbThumbAnimation = cdThumb.animate([{ transform: 'rotate(360deg)' }], {
duration: 10000,
iterations: Infinity,
});
cbThumbAnimation.pause();
// nextSong
nextBtn.onclick = function () {
if (_this.isRandom) {
_this.randomSong();
} else app.nextSong();
_this.activeSong();
audio.play();
};
// prevSong
prevBtn.onclick = function () {
if (_this.isRandom) {
_this.randomSong();
} else app.prevSong();
_this.activeSong();
audio.play();
};
// Turn on / Turn off randomSong
randomBtn.onclick = function () {
_this.isRandom = !_this.isRandom;
_this.setConfig('isRandom', _this.isRandom);
this.classList.toggle('active', _this.isRandom);
};
// Turn on / Turn off repeatSong
repeatBtn.onclick = function () {
_this.isRepeat = !_this.isRepeat;
_this.setConfig('isRepeat', _this.isRepeat);
this.classList.toggle('active', _this.isRepeat);
};
// auto nextSong when ended
audio.onended = function () {
if (_this.isRepeat) {
audio.play();
} else nextBtn.click();
};
// Lằng nghe hành vi click vào playList
// Closest trả về một element là chính nó hoặc thẻ cha của nó
// Nếu không tìm thấy thì return null
playList.onclick = function (e) {
const songNode = e.target.closest('.song:not(.active)'); // Song không có active
if (songNode || e.target.closest('.option')) {
// Xử lý khi click vào song
if (songNode) {
_this.currentIndex = Number(songNode.dataset.index);
_this.loadCurrentSong();
audio.play();
_this.activeSong();
}
}
};
// Volume
volumeIcon.onclick = function () {
_this.isMute = !_this.isMute;
// console.log({ volumeBar });
_this.setConfig('isMute', _this.isMute);
volumeIcon.classList.toggle('active', _this.isMute);
if (_this.isMute) {
audio.volume = 0;
volumeBar.value = 0;
} else {
volumeBar.value = _this.currentVolume * 100;
audio.volume = _this.currentVolume;
}
};
volumeBar.onchange = function (e) {
_this.currentVolume = e.target.value / 100;
audio.volume = _this.currentVolume;
_this.setConfig('currentVolume', _this.currentVolume);
audio.play();
if (audio.volume === 0) {
volumeIcon.classList.add('active');
} else {
_this.isMute = false;
_this.setConfig('isMute', _this.isMute);
volumeIcon.classList.remove('active');
}
};
},
timeFormat(seconds) {
const date = new Date(null);
date.setSeconds(seconds);
return date.toISOString().slice(14, 19);
},
nextSong: function () {
this.previousIndex = this.currentIndex;
this.currentIndex++;
if (this.currentIndex >= this.songs.length) {
this.currentIndex = 0;
}
this.loadCurrentSong();
},
prevSong: function () {
this.previousIndex = this.currentIndex;
this.currentIndex--;
if (this.currentIndex < 0) {
this.currentIndex = this.songs.length - 1;
}
this.loadCurrentSong();
},
randomSong: function () {
this.previousIndex = this.currentIndex;
let newIndex;
do {
newIndex = Math.floor(Math.random() * this.songs.length);
} while (songPlayedList.has(newIndex));
this.currentIndex = newIndex;
this.loadCurrentSong();
songPlayedList.add(newIndex);
if (songPlayedList.size === this.songs.length) {
songPlayedList.clear();
}
this.activeSong();
},
loadCurrentSong: function () {
heading.textContent = this.currentSong.name;
cdThumb.style.backgroundImage = `url('${this.currentSong.image}')`;
audio.src = this.currentSong.path;
},
loadConfig: function () {
this.isRandom = this.config.isRandom;
this.isRepeat = this.config.isRepeat;
this.currentVolume = this.config.currentVolume;
this.isMute = this.config.isMute;
this.currentIndex = this.config.currentIndex;
// Hiển thị trạng thái bang đầu của button repeat và random
randomBtn.classList.toggle('active', this.isRandom);
repeatBtn.classList.toggle('active', this.isRepeat);
volumeIcon.classList.toggle('active', this.isMute);
},
activeSong: function () {
const songs = $$('.song');
songs.forEach((song, index) => {
if (index === this.currentIndex) {
song.classList.add('active');
this.setConfig('currentIndex', this.currentIndex);
setTimeout(() => {
song.scrollIntoView({
behavior: 'smooth',
block: 'center',
inline: 'center',
});
}, 300);
} else song.classList.remove('active');
});
},
start: function () {
// Gán cấu hình đọc từ LOCAL_STORAGE_KEY từ config vào object app
this.loadConfig();
this.render();
this.defineProperties();
this.handleEvent();
this.loadCurrentSong();
},
};
app.start();