-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic.js
224 lines (192 loc) · 7.58 KB
/
music.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
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
/* pre defined music elements, stored in local directory */
const musicDB = [
{
number: 0,
name: "Crystal Skies & HALIENE - Stardust.mp3",
type: "audio/mpeg",
thumbnail: "./assets/demo-thumbnail/music0.jpg",
source: "./assets/demo-music/Crystal Skies & HALIENE - Stardust.mp3"
},
{
number: 1,
name: "Disclosure - You & Me Rivo Remix.mp3",
type: "audio/mpeg",
thumbnail: "./assets/demo-thumbnail/music1.jpg",
source: "./assets/demo-music/Disclosure - You & Me Rivo Remix.mp3"
},
{
number: 2,
name: "IVOXYGEN - HELLBOY.mp3",
type: "audio/mpeg",
thumbnail: "./assets/demo-thumbnail/music2.jpg",
source: "./assets/demo-music/IVOXYGEN - HELLBOY.mp3"
},
{
number: 3,
name: "Repiet & Julia Kleijn - Feels Like.mp3",
type: "audio/mpeg",
thumbnail: "./assets/demo-thumbnail/music3.jpg",
source: "./assets/demo-music/Repiet & Julia Kleijn - Feels Like.mp3"
},
{
number: 4,
name: "SAUDADE - ATMOSPHERE PHONK.mp3",
type: "audio/mpeg",
thumbnail: "./assets/demo-thumbnail/music4.jpg",
source: "./assets/demo-music/SAUDADE - ATMOSPHERE PHONK.mp3"
},
{
number: 5,
name: "Sea Of Problems.mp3",
type: "audio/mpeg",
thumbnail: "./assets/demo-thumbnail/music5.jpg",
source: "./assets/demo-music/Sea Of Problems.mp3"
}
]
var playButton = document.getElementById("playbtn");
var pauseButton = document.getElementById("pausebtn");
var shuffleButton = document.getElementById("shufflebtn");
var repeatButton = document.getElementById("repeatbtn");
var volumeOn = document.getElementById("volumeon");
var volumeOff = document.getElementById("volumeoff");
var volumeSlider = document.getElementById("volume");
var displayedTime = document.getElementById("playing-time");
var displayedDuration = document.getElementById("duration-time");
var displayedThumbnail = document.getElementById("thumbnail");
var displayedTitle1 = document.getElementById("songTitle1")
var displayedTitle2 = document.getElementById("songTitle2")
var progressSlider = document.getElementById("progress");
var firstTrack = true;
var lastVolumeState = 0;
var currentSongNumber = 0;
var totalTime = 0;
var shuffle = false;
function loadSong(musicNumber) {
currentSongNumber = musicNumber;
var audio = document.getElementById("currentAudio");
audio.src = "./assets/demo-music/" + music[musicNumber].name;
displayedThumbnail.style.backgroundImage = 'url(' + music[musicNumber].thumbnail + ')';
// styling of title in list window
var title = document.getElementById(`title${musicNumber}`).getElementsByTagName('a')[0];
title.style.color = getComputedStyle(document.documentElement).getPropertyValue('--slider-range-color')
// doubled title for infinity rotation
displayedTitle1.textContent = music[musicNumber].name.substring(-1, music[musicNumber].name.indexOf('.')) + " | "
displayedTitle2.textContent = music[musicNumber].name.substring(-1, music[musicNumber].name.indexOf('.')) + " | "
// default volume setting at start
if (firstTrack) {
audio.volume = 0.5;
firstTrack = false;
}
// duration of audio object can only be loaded from metadata
audio.addEventListener("loadedmetadata", () => {
displayedDuration.textContent = getSongTimeFormatted(audio.duration);
totalTime = audio.duration;
})
audio.addEventListener("timeupdate", () => {
displayedTime.textContent = getSongTimeFormatted(audio.currentTime);
// first try totalTime equals 0 which leads to irregular jump of slider
if(totalTime != 0) progressSlider.value = Math.floor((audio.currentTime / totalTime)*100)
})
}
function musicPlay() {
playButton.style.display = "none";
pauseButton.style.display = "inline"
var song = document.getElementById("currentAudio");
song.play();
updateVolume(song);
}
function musicPause() {
playButton.style.display = "inline";
pauseButton.style.display = "none";
var song = document.getElementById("currentAudio")
song.pause();
}
function musicPrevious() {
var nextSong;
if(shuffle) nextSong = getRandomSongNumber();
else nextSong = (currentSongNumber == 0) ? music.length-1 : currentSongNumber-1;
resetSelectedElementStyle(true);
loadSong(nextSong);
musicPlay();
}
function musicNext() {
var nextSong;
if(shuffle) nextSong = getRandomSongNumber();
else nextSong = (currentSongNumber == music.length-1) ? 0 : currentSongNumber+1;
resetSelectedElementStyle(true);
loadSong(nextSong);
musicPlay();
}
function musicShuffling() {
shuffleButton.style.color = getComputedStyle(document.documentElement).getPropertyValue('--shuffle-color');
shuffleButton.className = "icon-Shuffle mode";
repeatButton.style.color = getComputedStyle(document.documentElement).getPropertyValue('--btn-color');
repeatButton.className = "icon-Repeat normal";
shuffle = true;
document.getElementById("currentAudio").addEventListener("ended", ()=> {
musicNext();
});
}
function musicRepeating() {
repeatButton.style.color = getComputedStyle(document.documentElement).getPropertyValue('--repeat-color');
repeatButton.className = "icon-Repeat mode";
shuffleButton.style.color = getComputedStyle(document.documentElement).getPropertyValue('--btn-color');
shuffleButton.className = "icon-Shuffle normal";
shuffle = false;
document.getElementById("currentAudio").addEventListener("ended", ()=> {
musicNext();
});
}
function changeVolumeSymbol(vol) {
if(vol == 0) {
volumeOff.style.display = "inline";
volumeOn.style.display = "none";
} else {
volumeOff.style.display = "none";
volumeOn.style.display = "inline";
}
}
function quickUpdateVolume(mute = false) {
var audio = document.getElementById("currentAudio");
console.log("volume: ", audio.volume);
if (!mute) audio.volume = lastVolumeState;
else {
lastVolumeState = audio.volume;
audio.volume = 0;
}
volumeSlider.value = audio.volume*100;
changeVolumeSymbol(audio.volume);
}
function updateVolume(song) {
volumeSlider.addEventListener("input", (val) => {
song.volume = val.currentTarget.value/100;
})
}
function updateSongProgress(timeVal) {
document.getElementById("currentAudio").currentTime = (timeVal*totalTime)/100;
}
const getRandomSongNumber = () => Math.floor(Math.random() * (music.length-1));
function getSongTimeFormatted(time) {
const h = Math.floor(time / 3600).toString().padStart(2, '0'),
m = Math.floor(time % 3600 / 60).toString().padStart(2, '0'),
s = Math.floor(time % 60).toString().padStart(2, '0');
return `${h}:${m}:${s}`;
}
function resetPlayer() {
var song = document.getElementById("currentAudio");
if(song.src == "") return;
song.currentTime = 0;
song.src = "";
volumeSlider.value = 50;
firstTrack = true;
// thumbnail, title rotation and time values visually reset
document.getElementById("tools-inactive").style.display = "block";
displayedThumbnail.style.backgroundImage = 'url(./assets/images/happy-music.svg)'
displayedThumbnail.style.backgroundSize = 'contain';
displayedTime.textContent = "";
displayedTime.style.color = "transparent";
displayedDuration.textContent = "";
displayedDuration.style.color = "transparent";
displayedTitle1.textContent = "no music selected | ";
displayedTitle2.textContent = "no music selected | ";
}