-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
147 lines (128 loc) · 4.69 KB
/
Copy pathscript.js
File metadata and controls
147 lines (128 loc) · 4.69 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
window.addEventListener("DOMContentLoaded", () => {
const image = document.querySelector("img");
const title = document.getElementById("title");
const artist = document.getElementById("artist");
const audioElement = document.querySelector("audio");
const prevBtn = document.getElementById("prev");
const nextBtn = document.getElementById("forward");
const playBtn = document.getElementById("play");
const progressBar = document.getElementById("progress-container");
const progressIndicator = document.getElementById("progress");
const currentTimeElement = document.getElementById("current-time");
const durationElement = document.getElementById("duration");
const songs = [
{
name: "jacinto-1",
title: "Electric Chill Machine",
artist: "Jacinto Design",
},
{
name: "jacinto-2",
title: "Seven Nation Army (Remix)",
artist: "Jacinto Design",
},
{
name: "jacinto-3",
title: "Goodnight Disco Queen",
artist: "Jacinto Design",
},
{
name: "metric-1",
title: "Front Row (Remix)",
artist: "Metric/Jacinto Design",
},
];
let isSongPlaying = false;
let songIndex = 0; //set which song from list to play
const playSong = () => {
audioElement.play();
isSongPlaying = true;
playBtn.classList.replace("fa-play", "fa-pause");
playBtn.setAttribute("title", "Pause");
};
const pauseSong = () => {
audioElement.pause();
isSongPlaying = false;
playBtn.classList.replace("fa-pause", "fa-play");
playBtn.setAttribute("title", "Play");
};
//Update the DOM with song data
const loadSong = (song) => {
title.textContent = song.title;
artist.textContent = song.artist;
audioElement.src = `music/${song.name}.mp3`;
image.src = `img/${song.name}.jpg`;
};
const playPreviousSong = () => {
songIndex--;
if (songIndex < 0) {
songIndex = songs.length - 1;
}
loadSong(songs[songIndex]);
playSong();
};
const playNextSong = () => {
songIndex++;
if (songIndex > songs.length - 1) {
songIndex = 0;
}
loadSong(songs[songIndex]);
playSong();
};
//Calculate duration of the song in minutes
const getSongDurationInMinutes = (duration) => Math.floor(duration / 60);
//Calculate duration of the song in seconds
const getSongDurationInSeconds = (duration) => {
let durationInSeconds = Math.floor(duration % 60);
if (durationInSeconds < 10) {
durationInSeconds = `0${durationInSeconds}`;
}
return durationInSeconds.toString();
};
//Calculate time progress in percentage
const getProgressInPercentage = (duration, currentTime) => (currentTime / duration) * 100;
//Calculate current time of played song in minutes
const getCurrentTimeInMinutes = (currentTime) => Math.floor(currentTime / 60);
//Calculate current of the song in seconds
const getCurrentTimeInSeconds = (currentTime) => {
let currentInSeconds = Math.floor(currentTime % 60);
if (currentInSeconds < 10) {
currentInSeconds = `0${currentInSeconds}`;
}
return currentInSeconds.toString();
}
const updateProgressBar = (event) => {
if (isSongPlaying) {
const { duration, currentTime } = event.srcElement;
const progressPercent = getProgressInPercentage(duration, currentTime);
const durationInMinutes = getSongDurationInMinutes(duration);
const durationInSeconds = getSongDurationInSeconds(duration);
const currentInMinutes = getCurrentTimeInMinutes(currentTime);
const currentInSeconds = getCurrentTimeInSeconds(currentTime);
progressIndicator.style.width = `${progressPercent}%`;
//Wait for durationInSeconds to be calculated before changing DOM
if (durationInSeconds !== "NaN") {
durationElement.textContent = `${durationInMinutes}:${durationInSeconds}`;
}
currentTimeElement.textContent = `${currentInMinutes}:${currentInSeconds}`;
}
};
//Navigate a song by clicking progress bar
const navigateSong = (event) => {
const { duration } = audioElement;
const progressBarWidth = event.srcElement.clientWidth;
const barClickedPosition = event.offsetX;
audioElement.currentTime = (barClickedPosition / progressBarWidth) * duration;
!isSongPlaying && playSong();
};
//Select a first song on page load
loadSong(songs[songIndex]);
playBtn.addEventListener("click", () =>
isSongPlaying ? pauseSong() : playSong()
);
prevBtn.addEventListener("click", playPreviousSong);
nextBtn.addEventListener("click", playNextSong);
audioElement.addEventListener("timeupdate", updateProgressBar);
progressBar.addEventListener("click", navigateSong);
audioElement.addEventListener('ended', playNextSong);
});