-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmusicPlayer.js
More file actions
219 lines (185 loc) · 10.4 KB
/
Copy pathmusicPlayer.js
File metadata and controls
219 lines (185 loc) · 10.4 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
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
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡀⠀⠀⠀⠀⠀⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⡿⠇⠀⠀⠀⠀⢻⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡇⠀⠀⠀⠀⡸⣞⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠃⠀⠀⠀⢀⣧⢿⣽⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⢴⣿⠆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⣼⣞⡿⣞⡅⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠓⢤⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣾⠀⠀⠀⣰⣟⢾⣽⢫⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣠⢤⣶⡻⣞⣿⣺⢯⣽⣳⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⢠⣄⡀⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⣀⣠⣤⣿⣽⣻⢾⣽⣷⣾⣽⣻⣞⣷⣳⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠈⢻⣿⣶⣄⡀⠀⠀⠀⣉⣲⣴⢶⣞⡿⣽⣞⡷⣯⢿⡽⣞⣿⠟⠋⠁⠉⠈⠳⣟⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣿⣿⢶⣾⣿⡽⣯⣟⡾⣽⡷⣯⣟⡽⡾⣽⡯⠁⠀⠀⠀⠀⠀⠀⢮⣭⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⢞⣿⣿⢯⡿⣿⣯⣟⣷⣯⢿⣳⣟⡷⣽⣼⣻⣽⠀⠀⠀⠀⠀⠀⠀⢀⣼⡯⡗⠋⠤⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢾⣿⣿⣯⣽⣾⣿⣾⣗⡿⣯⡷⣯⣟⡷⣞⣼⣿⣀⠀⠀⠀⠀⢀⣠⡿⣏⡗⠈⠐⠈⠅⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⠛⠏⠉⠉⠽⢟⢿⣿⣿⣿⣿⣷⣻⢾⡽⣞⡷⠄⡹⣶⢿⣻⢿⣻⡽⢯⣼⢦⠶⠁⠈⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⣯⠇⠀⠀⠀⠀⠀⠁⣽⣿⣿⣿⣷⣯⣿⣽⣛⡦⠀⠀⢩⣿⣹⢯⣷⢻⣟⠺⢣⡖⣘⠤⠓⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢈⣿⡃⠁⠀⠀⠀⢀⣤⣾⣟⢿⣻⣿⣿⣟⡾⣽⡳⠄⠎⢳⣯⢯⣟⡾⢯⣞⣯⣓⠉⢀⠀⠀⡄⢢⡀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⣷⣷⣶⣳⣶⣺⣿⣿⣳⢯⣟⣿⣿⣳⢯⠛⠅⠃⠀⠀⣴⣿⡿⣬⢶⠾⠙⣊⣥⠾⡒⠊⢁⢠⠣⣌⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢺⡽⣾⡽⣯⣟⣿⡿⣯⣿⣿⣾⢿⣿⠳⢏⣈⢠⠀⠀⣰⢿⡿⣽⣉⡶⠌⠋⠉⣀⡀⠁⠀⠀⠀⣘⡐⣂⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣽⣳⣟⣳⣟⣾⣽⣿⣿⣿⣿⣿⣦⣜⡻⡽⠆⠧⣴⡟⣯⢟⡳⣭⠲⠄⠐⠀⠀⠀⠈⠁⠉⠑⢊⡕⢃⠄⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠹⣿⣾⣿⣯⣿⣾⣿⣿⣿⣿⣿⣿⣿⣿⣾⢧⠀⠹⠾⡵⡞⡽⢢⣃⠐⠀⠀⠄⡐⠀⠀⠀⡘⢦⠘⣌⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠐⠹⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢯⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠒⡈⠀⡀⠄⡑⠢⣉⠴⣈⣆
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢯⣏⡴⣶⣵⣢⢤⢠⡀⡄⢠⠐⡰⢌⡱⠀⡁⡀⠆⡥⠆⡥⣛⡽⣾
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠔⠉⠀⠀⢽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣼⣻⢷⣯⡽⣞⣷⣻⡼⣡⢋⡔⠣⠜⡐⢐⠠⡓⣤⣙⣲⣽⣻⢷
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡿⣽⣞⣷⣻⡴⣣⢜⡱⣊⡕⣊⠠⡙⡰⣭⢷⣯⣿⢿
//
// By THE 2e5d on discord
const playButton = document.getElementById('playBtn');
const previousButton = document.getElementById('prevBtn');
const nextButton = document.getElementById('nextBtn');
const progressBarElement = document.getElementById('progressBar');
const progressElement = document.getElementById('progress');
const currentTimeElement = document.getElementById('currentTime');
const totalTimeElement = document.getElementById('totalTime');
const songNameElement = document.getElementById('songName');
const albumArtElement = document.getElementById('art');
function initializeMusicPlayer() {
playButton.addEventListener('click', togglePlayback);
previousButton.addEventListener('click', () => { changeTrack(-1); });
nextButton.addEventListener('click', () => { changeTrack(1); });
progressBarElement.addEventListener('click', (event) => {
if (isDragging) return;
const barRect = progressBarElement.getBoundingClientRect();
const clickPosition = event.clientX - barRect.left;
const newProgress = (clickPosition / barRect.width) * 100;
currentProgress = Math.max(0, Math.min(100, newProgress));
progressElement.style.width = `${currentProgress}%`;
updateTimeDisplay();
if (audioElement && currentSongDuration > 0) {
const newTime = (currentProgress / 100) * currentSongDuration;
currentSeconds = newTime;
if (audioElement) {
audioElement.currentTime = currentSeconds;
}
}
});
progressBarElement.addEventListener('mousedown', startDrag);
document.addEventListener('keydown', (event) => {
if (event.code === 'Space') {
event.preventDefault();
togglePlayback();
} else if (event.code === 'ArrowLeft') {
changeTrack(-1);
} else if (event.code === 'ArrowRight') {
changeTrack(1);
}
});
}
function loadTrack(trackIndex) {
currentTrackIndex = trackIndex;
const track = config.musicPlayer.playlist[trackIndex];
songNameElement.textContent = track.title;
albumArtElement.style.backgroundImage = `url('${track.coverUrl}')`;
currentProgress = 0;
progressElement.style.width = '0%';
currentSeconds = 0;
currentSongDuration = 0;
updateTimeDisplay();
totalTimeElement.textContent = "0:00";
if (audioElement) {
audioElement.pause();
audioElement = null;
}
audioElement = new Audio(track.audioUrl);
audioElement.addEventListener('loadedmetadata', function() {
currentSongDuration = audioElement.duration;
const minutes = Math.floor(currentSongDuration / 60);
const seconds = Math.floor(currentSongDuration % 60);
totalTimeElement.textContent = `${minutes}:${seconds.toString().padStart(2, '0')}`;
});
audioElement.addEventListener('error', function() {
console.error('Error loading audio:', track.audioUrl);
totalTimeElement.textContent = "0:00";
});
if (isPlaying) {
startPlayback();
}
}
function changeTrack(direction) {
let newTrackIndex = currentTrackIndex + direction;
if (newTrackIndex < 0) {
newTrackIndex = config.musicPlayer.playlist.length - 1;
} else if (newTrackIndex >= config.musicPlayer.playlist.length) {
newTrackIndex = 0;
}
loadTrack(newTrackIndex);
if (isPlaying) {
startPlayback();
}
}
function togglePlayback() {
isPlaying = !isPlaying;
const playButtonSvg = playButton.querySelector('svg');
if (isPlaying) {
playButtonSvg.innerHTML = '<path fill="currentColor" d="M8 19c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2s-2 .9-2 2v10c0 1.1.9 2 2 2m6-12v10c0 1.1.9 2 2 2s2-.9 2-2V7c0-1.1-.9-2-2-2s-2 .9-2 2"/>';
startPlayback();
} else {
playButtonSvg.innerHTML = '<path fill="currentColor" d="M8 17.175V6.825q0-.425.3-.713t.7-.287q.125 0 .263.037t.262.113l8.15 5.175q.225.15.338.375t.112.475t-.112.475t-.338.375l-8.15 5.175q-.125.075-.262.113T9 18.175q-.4 0-.7-.288t-.3-.712"/>';
clearInterval(progressInterval);
if (audioElement) {
audioElement.pause();
}
}
}
function startPlayback() {
clearInterval(progressInterval);
if (audioElement) {
audioElement.currentTime = currentSeconds;
audioElement.play().catch(error => console.error('Playback error:', error));
}
progressInterval = setInterval(() => {
if (!isPlaying || isDragging || !audioElement) return;
if (audioElement.readyState >= 2) {
currentSeconds = audioElement.currentTime;
if (currentSongDuration > 0) {
currentProgress = (currentSeconds / currentSongDuration) * 100;
if (currentProgress >= 100) {
changeTrack(1);
} else {
progressElement.style.width = `${currentProgress}%`;
updateTimeDisplay();
}
} else {
progressElement.style.width = '0%';
updateTimeDisplay();
}
}
}, 500);
}
function updateTimeDisplay() {
if (currentSongDuration > 0) {
const currentTimeInSeconds = Math.floor((currentProgress / 100) * currentSongDuration);
const minutes = Math.floor(currentTimeInSeconds / 60);
const seconds = currentTimeInSeconds % 60;
currentTimeElement.textContent = `${minutes}:${seconds.toString().padStart(2, '0')}`;
} else {
currentTimeElement.textContent = "0:00";
}
}
function startDrag(event) {
isDragging = true;
event.preventDefault();
function updateDragPosition(moveEvent) {
if (!isDragging) return;
const barRect = progressBarElement.getBoundingClientRect();
const dragPosition = Math.max(0, Math.min(barRect.width, moveEvent.clientX - barRect.left));
const newProgress = (dragPosition / barRect.width) * 100;
currentProgress = Math.max(0, Math.min(100, newProgress));
progressElement.style.width = `${currentProgress}%`;
if (currentSongDuration > 0) {
currentSeconds = (currentProgress / 100) * currentSongDuration;
updateTimeDisplay();
if (audioElement) {
audioElement.currentTime = currentSeconds;
}
}
}
updateDragPosition(event);
function onMouseMove(event) { updateDragPosition(event); }
function stopDrag() {
isDragging = false;
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mouseup', stopDrag);
}
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', stopDrag);
}