-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
98 lines (83 loc) · 2.42 KB
/
index.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
let currentMusic = 0;
const music = document.querySelector('#audio');
const seekBar = document.querySelector('.seek-bar');
const songName = document.querySelector('.music-name');
const artistName = document.querySelector('.artist-name');
const disk = document.querySelector('.disk');
const currentTime = document.querySelector('.current-time');
const musicDuration = document.querySelector('.song-duration');
const playBtn = document.querySelector('.play-btn');
const forwardBtn = document.querySelector('.forward-btn');
const backwardBtn = document.querySelector('.backward-btn');
playBtn.addEventListener('click', () => {
if(playBtn.className.includes('pause')){
music.play();
} else{
music.pause();
}
playBtn.classList.toggle('pause');
disk.classList.toggle('play');
})
//setup music
const setMusic = (i) => {
seekBar.value = 0; //set range slide value to 0;
let song = songs [i];
currentMusic = i;
music.src = song.path;
songName.innerHTML = song.name;
artistName.innerHTML = song.artist;
disk.style.backgroundImage = `url('${song.cover}')`;
currentTime.innerHTML ='00:00';
setTimeout(() => {
seekBar.max = music.duration;
musicDuration.innerHTML = formatTime(music.duration);
}, 300);
}
setMusic(0);
//formating time in the min and second format
const formatTime = (time) => {
let min = Math.floor(time / 60);
if (min < 10){
min = `0${min}`;
}
let sec = Math.floor(time % 60);
if (sec > 10){
sec = `0${sec}`;
}
return `${min} : ${sec}`;
}
// seek bar
setInterval(() => {
seekBar.value = music.currentTime;
currentTime.innerHTML = formatTime(music.currentTime);
if(Math.floor(music.currentTime) == Math.floor(seekBar.max)){
forwardBtn.click();
}
}, 500)
seekBar.addEventListener('change', () => {
music.currentTime = seekBar.value;
})
const playMusic = () =>{
music.play();
playBtn.classList.remove('pause');
disk.classList.add('play');
}
//forward and backward button
forwardBtn.addEventListener('click', () => {
if(currentMusic >= songs.length - 1){
currentMusic = 0;
} else{
currentMusic++;
}
setMusic(currentMusic);
playMusic();
})
backwardBtn.addEventListener('click', () => {
if(currentMusic <= 0){
currentMusic = songs.length - 1;
} else{
currentMusic--;
}
setMusic(currentMusic);
playMusic();
})