-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.js
More file actions
434 lines (362 loc) · 17.4 KB
/
player.js
File metadata and controls
434 lines (362 loc) · 17.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import { stations as defaultStations } from './stations.js';
import { stateManager } from './state.js';
export function initPlayer() {
// DOM Elements
const stationSelect = document.getElementById('station-select');
const favoriteBtn = document.getElementById('favorite-btn');
const playPauseBtn = document.getElementById('play-pause-btn');
const popoutBtn = document.getElementById('popout-btn');
const volumeSlider = document.getElementById('volume-slider');
const nowPlayingStation = document.getElementById('now-playing-station');
const nowPlayingTrack = document.getElementById('now-playing-track');
const popoutNotice = document.getElementById('popout-notice');
const PROXY_URL = 'https://api.djay.ca/';
// 1. Audio Infrastructure Setup MUST happen before any state subscriptions that might use it
let currentState = stateManager.getState();
if (!currentState.audio) {
const audio = new Audio();
audio.crossOrigin = 'anonymous';
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const source = audioContext.createMediaElementSource(audio);
source.connect(audioContext.destination);
// Save infrastructure to state instantly
stateManager.setAudioInfrastructure(audio, audioContext, source, null, null);
}
// --- State Reactivity (The Waiter's Headset) ---
stateManager.subscribe((newState, oldState) => {
// Play/Pause Changed
if (newState.isPlaying !== oldState.isPlaying) {
updatePlayPauseIcon(newState.isPlaying);
updateMediaSession(newState);
if (newState.isPlaying && newState.audio) {
const currentAudio = newState.audio;
if (newState.audioContext && newState.audioContext.state === 'suspended') {
newState.audioContext.resume();
}
currentAudio.play().catch(err => {
console.error('Playback failed:', err);
if (nowPlayingTrack) nowPlayingTrack.textContent = 'Error: Unable to play stream';
// Revert state if play failed
stateManager.setPlaying(false);
});
} else if (!newState.isPlaying && newState.audio) {
newState.audio.pause();
}
updateNowPlaying(newState);
}
// Volume Changed
if (newState.volume !== oldState.volume && newState.audio) {
updateVolumeSliderTrack(newState.volume);
newState.audio.volume = newState.volume;
// Also keep the UI slider in sync if the state was changed programmatically via Media Keys
if (volumeSlider.value != newState.volume) {
volumeSlider.value = newState.volume;
}
}
// Station Changed
if (newState.currentStation !== oldState.currentStation) {
if (newState.audio) {
newState.audio.src = getProxiedAudioUrl(newState.currentStation);
}
// Sync the dropdown UI
if (stationSelect.value !== newState.currentStation) {
stationSelect.value = newState.currentStation;
}
updateFavoriteBtnState(newState.currentStation);
updateNowPlaying(newState);
if (newState.isPlaying && newState.audio) {
newState.audio.play().catch(err => {
console.error('Playback failed:', err);
if (nowPlayingTrack) nowPlayingTrack.textContent = 'Error: Unable to play stream';
stateManager.setPlaying(false);
});
}
}
// Popout Changed
if (newState.popoutWindow !== oldState.popoutWindow) {
const playerContent = document.querySelector('.radiostream-player .player-content');
if (newState.popoutWindow) {
playerContent.style.display = 'none';
if (popoutNotice) popoutNotice.style.display = 'block';
} else {
playerContent.style.display = 'flex';
if (popoutNotice) popoutNotice.style.display = 'none';
}
}
});
function populateStations() {
const state = stateManager.getState();
const currentVal = stationSelect.value;
stationSelect.innerHTML = '';
const favorites = JSON.parse(localStorage.getItem('favoriteStations')) || [];
const customStations = JSON.parse(localStorage.getItem('customStations')) || [];
const allStations = [...defaultStations, ...customStations];
const showFavoritesOnly = localStorage.getItem('favoritesOnly') === 'true';
let filteredStations = allStations;
if (showFavoritesOnly) {
filteredStations = filteredStations.filter(s => favorites.includes(s.url));
}
const selectedGenre = localStorage.getItem('selectedGenre') || 'all';
if (selectedGenre !== 'all') {
filteredStations = filteredStations.filter(s => s.genre === selectedGenre);
}
filteredStations.forEach((station) => {
const option = document.createElement('option');
option.value = station.url;
const isFav = favorites.includes(station.url);
option.textContent = (isFav ? '★ ' : '') + station.name;
option.dataset.genre = station.genre || '';
option.dataset.country = station.country || '';
stationSelect.appendChild(option);
});
if (currentVal && Array.from(stationSelect.options).some(opt => opt.value === currentVal)) {
stationSelect.value = currentVal;
} else if (filteredStations.length > 0 && !state.currentStation) {
// Initial load edge case: no station in state yet
stationSelect.value = filteredStations[0].url;
stateManager.setStation(filteredStations[0].url);
}
updateFavoriteBtnState(stateManager.getState().currentStation);
updateMediaSession(stateManager.getState());
}
populateStations();
window.addEventListener('stationListUpdated', populateStations);
function updatePlayPauseIcon(isPlaying) {
const playIcon = playPauseBtn.querySelector('.icon-play');
const pauseIcon = playPauseBtn.querySelector('.icon-pause');
playIcon.style.display = isPlaying ? 'none' : 'block';
pauseIcon.style.display = isPlaying ? 'block' : 'none';
}
function updateVolumeSliderTrack(value) {
const progress = value * 100;
const bg = `linear-gradient(to right, var(--primary-color) ${progress}%, var(--border-color) ${progress}%)`;
volumeSlider.style.background = bg;
}
function updateFavoriteBtnState(currentUrl) {
if (!favoriteBtn || !currentUrl) return;
const favorites = JSON.parse(localStorage.getItem('favoriteStations')) || [];
const isFav = favorites.includes(currentUrl);
favoriteBtn.classList.toggle('active', isFav);
favoriteBtn.title = isFav ? "Remove from Favorites" : "Add to Favorites";
}
function updateMediaSession(state) {
if (!('mediaSession' in navigator)) return;
const selectedOption = stationSelect.options[stationSelect.selectedIndex];
if (!selectedOption) return;
const stationName = selectedOption.text.replace(/^★\s/, '');
const genre = selectedOption.dataset.genre || 'Live Radio';
const country = selectedOption.dataset.country || 'Internet';
navigator.mediaSession.metadata = new MediaMetadata({
title: stationName,
artist: genre,
album: country,
artwork: [
{ src: 'https://cdn-icons-png.flaticon.com/512/565/565535.png', sizes: '512x512', type: 'image/png' }
]
});
navigator.mediaSession.playbackState = state.isPlaying ? 'playing' : 'paused';
}
function getProxiedAudioUrl(url) {
if (!url) return '';
return `${PROXY_URL}?url=${encodeURIComponent(url)}`;
}
// Initial explicit UI sync based on starting state
currentState = stateManager.getState();
if (!currentState.currentStation) {
stateManager.setStation(stationSelect.value);
}
// Set explicit starting volumes so UI catches up
volumeSlider.value = currentState.volume;
updateVolumeSliderTrack(currentState.volume);
currentState.audio.volume = currentState.volume;
updatePlayPauseIcon(currentState.isPlaying);
if (currentState.currentStation) {
currentState.audio.src = getProxiedAudioUrl(currentState.currentStation);
}
// --- Audio Event Listeners (Auto-Reconnect) ---
let reconnectTimeout = null;
function handleStreamDrop() {
const state = stateManager.getState();
if (!state.isPlaying) return;
if (nowPlayingTrack) {
nowPlayingTrack.textContent = "Reconnecting...";
nowPlayingTrack.classList.remove('marquee-active');
}
if (reconnectTimeout) clearTimeout(reconnectTimeout);
console.log("Stream dropped. Attempting to reconnect in 3 seconds...");
reconnectTimeout = setTimeout(() => {
const currentState = stateManager.getState();
if (currentState.isPlaying) {
console.log("Reconnecting...");
currentState.audio.src = getProxiedAudioUrl(currentState.currentStation);
currentState.audio.load();
currentState.audio.play().catch(err => {
console.error('Reconnect failed:', err);
if (nowPlayingTrack) nowPlayingTrack.textContent = 'Reconnect failed. Retry play.';
stateManager.setPlaying(false);
});
}
}, 3000);
}
currentState.audio.addEventListener('error', () => { handleStreamDrop(); });
currentState.audio.addEventListener('ended', () => { handleStreamDrop(); });
currentState.audio.addEventListener('playing', () => {
if (reconnectTimeout) {
clearTimeout(reconnectTimeout);
reconnectTimeout = null;
}
const st = stateManager.getState();
if (st.isPlaying && nowPlayingTrack && (nowPlayingTrack.textContent === "Reconnecting..." || nowPlayingTrack.textContent.includes("Reconnect failed"))) {
fetchMetadata(st.currentStation);
}
});
// --- User UI Interactions ---
playPauseBtn.addEventListener('click', () => {
const isCurrentlyPlaying = stateManager.getState().isPlaying;
stateManager.setPlaying(!isCurrentlyPlaying);
});
stationSelect.addEventListener('change', () => {
stateManager.setStation(stationSelect.value);
});
volumeSlider.addEventListener('input', () => {
stateManager.setVolume(volumeSlider.value);
});
if ('mediaSession' in navigator) {
navigator.mediaSession.setActionHandler('play', () => stateManager.setPlaying(true));
navigator.mediaSession.setActionHandler('pause', () => stateManager.setPlaying(false));
navigator.mediaSession.setActionHandler('stop', () => stateManager.setPlaying(false));
navigator.mediaSession.setActionHandler('previoustrack', () => {
let newIndex = stationSelect.selectedIndex - 1;
if (newIndex < 0) newIndex = stationSelect.options.length - 1;
stationSelect.selectedIndex = newIndex;
stateManager.setStation(stationSelect.value);
});
navigator.mediaSession.setActionHandler('nexttrack', () => {
let newIndex = stationSelect.selectedIndex + 1;
if (newIndex >= stationSelect.options.length) newIndex = 0;
stationSelect.selectedIndex = newIndex;
stateManager.setStation(stationSelect.value);
});
}
favoriteBtn?.addEventListener('click', () => {
const currentUrl = stateManager.getState().currentStation;
let favorites = JSON.parse(localStorage.getItem('favoriteStations')) || [];
if (favorites.includes(currentUrl)) {
favorites = favorites.filter(url => url !== currentUrl);
} else {
favorites.push(currentUrl);
let customStations = JSON.parse(localStorage.getItem('customStations')) || [];
const allKnown = [...defaultStations, ...customStations];
if (!allKnown.some(s => s.url === currentUrl)) {
const selectedOption = stationSelect.options[stationSelect.selectedIndex];
const name = selectedOption ? selectedOption.text.replace(/^★\s/, '') : 'Saved Station';
const genre = selectedOption ? (selectedOption.dataset.genre || '') : '';
customStations.push({ name, url: currentUrl, genre });
localStorage.setItem('customStations', JSON.stringify(customStations));
}
}
localStorage.setItem('favoriteStations', JSON.stringify(favorites));
populateStations();
window.dispatchEvent(new CustomEvent('stationListUpdated'));
});
// --- Popout Logic ---
if (popoutBtn) {
popoutBtn.addEventListener('click', () => {
const st = stateManager.getState();
if (st.popoutWindow && !st.popoutWindow.closed) {
st.popoutWindow.focus();
return;
}
if (st.isPlaying) {
stateManager.setPlaying(false);
}
const themeClass = document.documentElement.classList.contains('dark-theme') ? 'dark' : 'light';
const popoutUrl = `popout.html?station=${encodeURIComponent(st.currentStation)}&theme=${themeClass}`;
const newWindow = window.open(popoutUrl, 'RadioStreamPopout', 'width=300,height=278');
stateManager.setPopoutWindow(newWindow);
const popoutCheckInterval = setInterval(() => {
const updatedState = stateManager.getState();
if (updatedState.popoutWindow && updatedState.popoutWindow.closed) {
clearInterval(popoutCheckInterval);
handlePopoutClose();
}
}, 500);
});
window.addEventListener('message', (event) => {
if (event.data.type === 'popoutClosed') {
handlePopoutClose();
}
});
}
window.addEventListener('beforeunload', cleanup);
function handlePopoutClose() {
const st = stateManager.getState();
if (!st.popoutWindow && !window.opener) return;
stateManager.setPopoutWindow(null);
stateManager.setPlaying(true);
}
// --- Metadata Polling ---
async function fetchMetadata(streamUrl) {
if (!nowPlayingTrack) return;
try {
const response = await fetch(`${PROXY_URL}metadata?url=${encodeURIComponent(streamUrl)}`);
const data = await response.json();
if (data.title) {
nowPlayingTrack.textContent = data.title;
if (nowPlayingTrack.scrollWidth > nowPlayingTrack.parentElement.clientWidth) {
nowPlayingTrack.classList.add('marquee-active');
} else {
nowPlayingTrack.classList.remove('marquee-active');
}
} else {
const stationName = stationSelect.options[stationSelect.selectedIndex]?.text.replace(/^★\s/, '') || 'Live Stream';
nowPlayingTrack.textContent = stationName;
nowPlayingTrack.classList.remove('marquee-active');
}
} catch (error) {
console.error("Failed to fetch stream metadata:", error);
nowPlayingTrack.textContent = stationSelect.options[stationSelect.selectedIndex]?.text.replace(/^★\s/, '');
nowPlayingTrack.classList.remove('marquee-active');
}
updateMediaSession(stateManager.getState());
}
function updateNowPlaying(state) {
if (!nowPlayingStation || !nowPlayingTrack) return;
const selectedOption = stationSelect.options[stationSelect.selectedIndex];
if (!selectedOption) return;
const stationName = selectedOption.text.replace(/^★\s/, '');
nowPlayingStation.textContent = `Now Playing: ${stationName}`;
if (state.metadataInterval) {
clearInterval(state.metadataInterval);
stateManager.setMetadataInterval(null);
}
const streamUrl = state.currentStation;
if (state.isPlaying) {
nowPlayingTrack.textContent = "Loading track info...";
nowPlayingTrack.classList.remove('marquee-active');
fetchMetadata(streamUrl);
} else {
nowPlayingTrack.textContent = "Ready to play...";
nowPlayingTrack.classList.remove('marquee-active');
updateMediaSession(state);
}
const newInterval = setInterval(() => {
if (stateManager.getState().isPlaying) {
fetchMetadata(streamUrl);
}
}, 12000);
stateManager.setMetadataInterval(newInterval);
}
function cleanup() {
const st = stateManager.getState();
if (st.isPlaying) {
stateManager.setPlaying(false);
}
if (st.animationFrameId) {
cancelAnimationFrame(st.animationFrameId);
}
if (st.metadataInterval) {
clearInterval(st.metadataInterval);
}
}
}