-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (141 loc) · 4.41 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
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
import { songs } from "./songs.js";
const songTitle = document.querySelector(".info h3");
const artistTitle = document.querySelector(".info p");
const currentTime = document.querySelector(".currentTime")
const songDuration = document.querySelector(".duration");
const discImage = document.querySelector(".disc");
const progressBar = document.querySelector(".progress");
const controls = document.querySelectorAll(".controls > i");
const newSong = new Audio;
newSong.volume = "0.05"
let currentSong = 0
let songPlaying = false
function showInfo(songIndex = 0) {
const [{ songName, artistName, duration, albumCover, songSrc }] = [songs[songIndex]]
songTitle.textContent = songName
artistTitle.textContent = artistName
currentTime.textContent = "0:00"
songDuration.textContent = duration
discImage.style.background = `url(${albumCover}) no-repeat`
discImage.style.backgroundSize = "cover"
newSong.src = songSrc
}
showInfo()
controls.forEach((button, index) => {
button.addEventListener("click", () => {
switch (index) {
case 0:
prevSong()
break;
case 1:
playPauseSong()
break;
case 2:
nextSong()
}
})
})
function prevSong() {
if (currentSong === 0) {
currentSong = songs.length - 1
}else if(currentSong > 0) {
currentSong--
}
showInfo(currentSong)
if (songPlaying) {
newSong.play()
}
}
function nextSong() {
if (currentSong === songs.length - 1) {
currentSong = 0
}else if(currentSong < songs.length - 1) {
currentSong++
}
showInfo(currentSong)
if (songPlaying) {
newSong.play()
}
}
function playPauseSong() {
if (!songPlaying) {
songPlaying = true
discImage.classList.add("playing")
controls[1].classList.remove("fa-play")
controls[1].classList.toggle("fa-pause", songPlaying)
newSong.play()
updateSongTime()
}else {
songPlaying = false
newSong.pause()
controls[1].classList.add("fa-play")
discImage.classList.remove("playing")
}
console.log(newSong.duration);
console.log(newSong.currentTime);
}
const barWidth = progressBar.parentElement.scrollWidth
let progressWidth;
function updateSongTime() {
newSong.addEventListener('timeupdate', () => {
newSong.addEventListener("ended", () => {
if (newSong.currentTime === newSong.duration) {
if (currentSong === songs.length - 1) {
currentSong = -1
return
}
currentSong++
showInfo(currentSong)
newSong.play()
return
}
})
const pixelRate = barWidth / newSong.duration
progressWidth = Math.floor(newSong.currentTime * pixelRate)
progressBar.style.width = `${progressWidth}px`
let minutes = Math.floor(newSong.currentTime / 60);
let seconds = Math.floor(newSong.currentTime % 60);
currentTime.textContent = `${minutes}:${seconds.toString().padStart(2, "0")}`;
});
}
progressBar.parentElement.addEventListener("click", (e) => {
const progressRect = progressBar.getBoundingClientRect()
const mouseX = e.clientX
const result = mouseX - progressRect.left
const progressBarWidth = progressBar.parentElement.scrollWidth
const progressPercentage = (result / progressBarWidth) * 100;
const newTime = (progressPercentage / 100) * newSong.duration;
progressWidth = result
progressBar.style.width = `${progressPercentage}%`
newSong.currentTime = newTime
updateSongTime()
})
const volumeBar = document.querySelector(".volume-controls input");
const volumeButton = document.querySelector(".volume-controls .fa-volume-high");
volumeButton.addEventListener("click", () => {
newSong.volume = 0
volumeButton.classList.toggle("fa-volume-xmark")
initializeVolume()
})
function unMute() {
if (newSong.volume > 0.02) {
volumeButton.classList.remove("fa-volume-xmark")
}
}
volumeBar.addEventListener("click", () => {
unMute()
})
volumeBar.addEventListener("input", () => {
if (newSong.volume == 0.02) {
volumeButton.classList.add("fa-volume-xmark")
}
unMute()
newSong.volume = volumeBar.value / 100
initializeVolume()
})
function initializeVolume() {
const widthBar = newSong.volume * 100
volumeBar.value = widthBar
volumeBar.style.background = `linear-gradient(to right, #10A351 ${widthBar}%, #ffffffa9 0%)`;
}
initializeVolume()