Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
302 lines (262 loc) · 11.3 KB
/
Copy pathscript.js
File metadata and controls
302 lines (262 loc) · 11.3 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Gambar cadangan saat lagu tidak punya albumArt.
// Sebelumnya menunjuk 'placeholder.jpg' yang tidak ada di repo -> gambar rusak.
const FALLBACK_ART = 'thumbnail.jpg';
const NO_LYRICS_TEXT = 'Lirik tidak tersedia';
// Pemutar ini khusus playlist internal (file lokal di folder music/).
// Dukungan YouTube sudah dihapus: satu-satunya sumber audio adalah <audio>.
class MusicPlayer {
constructor() {
this.audio = document.getElementById('audio');
this.playPauseBtn = document.getElementById('playPauseBtn');
this.prevBtn = document.getElementById('prevBtn');
this.nextBtn = document.getElementById('nextBtn');
this.progressBar = document.getElementById('progressBar');
this.volumeBar = document.getElementById('volumeBar');
this.currentTimeDisplay = document.getElementById('currentTime');
this.durationDisplay = document.getElementById('duration');
this.songTitle = document.getElementById('songTitle');
this.artist = document.getElementById('artist');
this.albumArt = document.getElementById('albumArt');
this.backgroundBlur = document.getElementById('backgroundBlur');
this.currentLyric = document.getElementById('currentLyric');
this.nextLyric = document.getElementById('nextLyric');
this.futureLyric = document.getElementById('futureLyric');
this.playlistSelect = document.getElementById('playlistSelect');
this.playlistTitle = document.getElementById('playlistTitle');
this.currentSongIndex = 0;
this.isPlaying = false;
this.allPlaylists = {};
this.playlist = [];
this.lyrics = [];
this.currentLyricIndex = 0;
this.init();
}
async init() {
await this.loadConfig();
this.setupEventListeners();
this.setVolume();
const defaultPlaylistName = (this.allPlaylists && (this.allPlaylists.defaultPlaylist || Object.keys(this.allPlaylists.playlists || {})[0]));
if (defaultPlaylistName) {
this.loadSpecificPlaylist(defaultPlaylistName);
} else {
console.error('No playlists found in config.');
}
}
async loadConfig() {
try {
const response = await fetch('config.json');
const config = await response.json();
this.allPlaylists = config;
const playlistNames = Object.keys(config.playlists || {});
this.playlistSelect.innerHTML = '';
playlistNames.forEach(name => {
const option = document.createElement('option');
option.value = name;
option.textContent = name;
this.playlistSelect.appendChild(option);
});
if (config.defaultPlaylist) {
this.playlistSelect.value = config.defaultPlaylist;
}
if (config.playlistName) {
this.playlistTitle.textContent = config.playlistName;
} else if (config.defaultPlaylist) {
this.playlistTitle.textContent = config.defaultPlaylist;
}
} catch (error) {
console.error('Error loading config:', error);
}
}
setupEventListeners() {
this.playPauseBtn.addEventListener('click', () => this.togglePlay());
this.prevBtn.addEventListener('click', () => this.playPrevious());
this.nextBtn.addEventListener('click', () => this.playNext());
this.audio.addEventListener('timeupdate', () => this.updateProgress());
this.audio.addEventListener('loadedmetadata', () => this.updateDuration());
this.audio.addEventListener('ended', () => this.playNext());
this.progressBar.addEventListener('input', () => this.seek());
this.volumeBar.addEventListener('input', () => this.setVolume());
this.playlistSelect.addEventListener('change', (event) => {
this.loadSpecificPlaylist(event.target.value);
});
const startButton = document.getElementById('startButton');
const welcomePanel = document.getElementById('welcomePanel');
startButton.addEventListener('click', () => {
welcomePanel.classList.add('hidden');
this.startPlayback();
});
// Jaring pengaman: browser memblokir autoplay sampai ada interaksi,
// jadi klik pertama di mana pun ikut memicu pemutaran.
document.addEventListener('click', () => {
if (!this.isPlaying && welcomePanel.classList.contains('hidden')) {
this.startPlayback();
}
}, { once: true });
}
startPlayback() {
this.audio.play().catch(() => {});
this.isPlaying = true;
this.playPauseBtn.innerHTML = '<i class="fas fa-pause"></i>';
}
loadSpecificPlaylist(playlistName) {
if (this.allPlaylists.playlists && this.allPlaylists.playlists[playlistName]) {
this.playlist = this.allPlaylists.playlists[playlistName];
this.currentSongIndex = 0;
this.loadSong(this.currentSongIndex);
this.playlistTitle.textContent = playlistName;
} else {
console.error(`Playlist "${playlistName}" not found.`);
}
}
loadSong(index) {
if (this.playlist.length === 0) {
this.songTitle.textContent = 'No songs in playlist';
this.artist.textContent = '';
this.albumArt.src = FALLBACK_ART;
this.audio.src = '';
this.clearLyrics();
return;
}
this.currentSongIndex = index;
const song = this.playlist[index];
const albumArt = song.albumArt || FALLBACK_ART;
this.audio.src = song.path || '';
this.songTitle.textContent = song.title || 'Unknown';
this.artist.textContent = song.artist || '';
this.albumArt.src = albumArt;
this.backgroundBlur.style.backgroundImage = `url(${albumArt})`;
if (song.lyricsPath) this.loadLyrics(song.lyricsPath);
else this.clearLyrics();
if (this.isPlaying) this.audio.play().catch(() => {});
}
clearLyrics() {
this.lyrics = [];
this.currentLyricIndex = 0;
this.currentLyric.textContent = NO_LYRICS_TEXT;
this.nextLyric.textContent = '';
this.futureLyric.textContent = '';
}
async loadLyrics(lyricsPath) {
if (!lyricsPath) {
this.clearLyrics();
return;
}
try {
const response = await fetch(lyricsPath);
if (!response.ok) throw new Error('Lyrics file not found or could not be loaded.');
const lrcText = await response.text();
this.lyrics = this.parseLRC(lrcText);
this.currentLyricIndex = 0;
if (this.lyrics.length === 0) this.clearLyrics();
} catch (error) {
console.error('Error loading lyrics:', error);
this.clearLyrics();
}
}
parseLRC(lrcText) {
const lines = lrcText.split('\n');
const lyrics = [];
// Menerima [m:ss], [mm:ss], [mm:ss.xx] dan [mm:ss.xxx].
// Pemisah pecahan boleh '.' atau ':' (varian LRC yang umum di luar sana).
const stampRegex = /\[(\d{1,3}):(\d{2})(?:[.:](\d{2,3}))?\]/g;
lines.forEach(line => {
stampRegex.lastIndex = 0;
const stamps = [...line.matchAll(stampRegex)];
if (stamps.length === 0) return;
// Tag metadata seperti [ti:...] / [ar:...] tidak akan cocok karena
// grup pertama hanya menerima angka.
const text = line.slice(stamps[stamps.length - 1].index + stamps[stamps.length - 1][0].length).trim();
// Satu baris bisa punya beberapa timestamp (lirik berulang).
stamps.forEach(stamp => {
const minutes = parseInt(stamp[1], 10);
const seconds = parseInt(stamp[2], 10);
const fraction = stamp[3] || '0';
// 2 digit = ratusan detik (x10), 3 digit = milidetik apa adanya.
const milliseconds = fraction.length === 3
? parseInt(fraction, 10)
: parseInt(fraction, 10) * 10;
const time = minutes * 60 + seconds + milliseconds / 1000;
lyrics.push({ time, text });
});
});
return lyrics.sort((a, b) => a.time - b.time);
}
updateLyrics() {
if (this.lyrics.length === 0) {
this.currentLyric.textContent = NO_LYRICS_TEXT;
this.nextLyric.textContent = '';
this.futureLyric.textContent = '';
return;
}
const currentTime = this.getCurrentTime();
let found = false;
for (let i = 0; i < this.lyrics.length; i++) {
if (currentTime < this.lyrics[i].time) {
this.currentLyricIndex = Math.max(0, i - 1);
found = true;
break;
}
}
if (!found) this.currentLyricIndex = this.lyrics.length - 1;
this.currentLyric.textContent = this.lyrics[this.currentLyricIndex]?.text || '';
this.nextLyric.textContent = this.lyrics[this.currentLyricIndex + 1]?.text || '';
this.futureLyric.textContent = this.lyrics[this.currentLyricIndex + 2]?.text || '';
}
togglePlay() {
if (this.isPlaying) {
this.audio.pause();
this.playPauseBtn.innerHTML = '<i class="fas fa-play"></i>';
this.isPlaying = false;
} else {
this.audio.play().catch(() => {});
this.playPauseBtn.innerHTML = '<i class="fas fa-pause"></i>';
this.isPlaying = true;
}
}
playPrevious() {
if (this.playlist.length === 0) return;
this.currentSongIndex = (this.currentSongIndex - 1 + this.playlist.length) % this.playlist.length;
this.loadSong(this.currentSongIndex);
}
playNext() {
if (this.playlist.length === 0) return;
this.currentSongIndex = (this.currentSongIndex + 1) % this.playlist.length;
this.loadSong(this.currentSongIndex);
}
getCurrentTime() {
return this.audio.currentTime || 0;
}
getDuration() {
const duration = this.audio.duration;
return isFinite(duration) ? duration : 0;
}
updateProgress() {
const currentTime = this.getCurrentTime();
const duration = this.getDuration();
const progress = (duration > 0) ? (currentTime / duration) * 100 : 0;
this.progressBar.value = progress || 0;
this.currentTimeDisplay.textContent = this.formatTime(currentTime);
this.updateLyrics();
}
updateDuration() {
this.durationDisplay.textContent = this.formatTime(this.getDuration());
}
seek() {
const duration = this.getDuration();
if (duration <= 0) return;
this.audio.currentTime = (this.progressBar.value / 100) * duration;
}
setVolume() {
const val = parseInt(this.volumeBar.value, 10) || 0;
this.audio.volume = val / 100;
}
formatTime(seconds) {
if (!isFinite(seconds) || Number.isNaN(seconds)) seconds = 0;
const minutes = Math.floor(seconds / 60);
const remainingSeconds = Math.floor(seconds % 60);
return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}`;
}
}
document.addEventListener('DOMContentLoaded', () => {
new MusicPlayer();
});