-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
177 lines (153 loc) · 4.21 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
const music = document.querySelector("audio");
const prevBtn = document.getElementById("prev");
const playBtn = document.getElementById("play");
const nextBtn = document.getElementById("next");
//Variables for changing the song
const image = document.querySelector('img');
const title = document.getElementById("title");
const artist = document.getElementById("artist");
//Progress bar
const progressContainer = document.getElementById("progress-container");
const progressBar = document.getElementById("progress");
//Time elements
const current = document.getElementById("current-time");
const durationTime = document.getElementById("duration");
//Music
const songs = [
{
name: "savannah",
displayName: "Savannah",
artist: "Diviners"
},
{
name: "link",
displayName: "Link",
artist: "Jim Yosef"
},
{
name: "tropic",
displayName: "Tropic Love",
artist: "Diviners feat. Contracreast"
},
{
name: "eclipse",
displayName: "Eclipse",
artist: "Jim Yosef"
},
];
//Check if Playing
function isPlaying(music) {
return !music.paused;
}
//Play or pause
function Toggle() {
if (!isPlaying(music)) {
playSong();
}
else {
pauseSong();
}
}
//Play song
function playSong() {
playBtn.classList.replace("fa-play", "fa-pause");
playBtn.setAttribute("title", "Pause");
music.play();
}
//Pause song
function pauseSong() {
playBtn.classList.replace("fa-pause", "fa-play");
playBtn.setAttribute("title", "Play");
music.pause();
}
//Play or pause event listener
playBtn.addEventListener("click", Toggle);
//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 currentSong = 0;
//Get to previous song
function prevSong() {
if (currentSong == 0) {
currentSong = songs.length-1;
}
else {
currentSong--;
}
console.log(currentSong);
loadSong(songs[currentSong]);
playSong();
}
//Next song
function nextSong() {
if (currentSong == songs.length-1) {
currentSong = 0;
}
else {
currentSong++;
}
loadSong(songs[currentSong]);
playSong();
}
//Change song
prevBtn.addEventListener("click", prevSong);
nextBtn.addEventListener("click", nextSong);
//Update progress bar
function updateProgressBar(event) {
if (isPlaying) {
const { duration, currentTime } = event.target;
//Update progress bar
const progressPercent = (currentTime/ duration) * 100;
progressBar.style.width = `${progressPercent}%`;
//Calculate the display for the duration
const durationMinutes = Math.floor(duration/60);
let durationSeconds = Math.floor(duration % 60);
if (durationSeconds < 10) {
durationSeconds = `0${durationSeconds}`;
}
//Delay switching duration of Element to avoid NaN
if (durationSeconds) {
durationTime.textContent = `${durationMinutes}: ${durationSeconds}`;
}
//Calculate the display for the current time
let minutes = Math.floor(currentTime/60);
let seconds = Math.floor(currentTime % 60);
if (seconds < 10) {
seconds = `0${seconds}`;
}
current.textContent = `${minutes}: ${seconds}`;
}
}
//Change progress Bar
music.addEventListener("timeupdate", updateProgressBar);
//Change bar depending on clicks
function setProgressBar(event) {
let width = this.clientWidth;
let offsX = event.offsetX;
let percent = offsX/width;
const { duration } = music;
let placeInSong = percent * duration;
music.currentTime = Math.floor(placeInSong);
}
//Check for clicks in progress bar
progressContainer.addEventListener("click", setProgressBar);
//Move to next song
function moveToNext() {
if (currentSong == songs.length-1){
currentSong = 0;
loadSong(songs[currentSong]);
playSong();
}
else {
currentSong++;
loadSong(songs[currentSong]);
playSong();
}
}
//Check if song has ended
music.addEventListener("ended", moveToNext);