vers. 2.3.0

This commit is contained in:
2026-02-06 22:13:10 +01:00
parent ae5a4a8153
commit 2b0b457166
9 changed files with 137 additions and 47 deletions

View File

@@ -340,12 +340,14 @@ document.addEventListener('DOMContentLoaded', function () {
*/
function initializePlayer() {
console.log('Inizializzazione player audio (v2.0 - Hybrid Strategy)...');
console.log('Inizializzazione player audio (v2.1 - Format Toggle)...');
const playPauseBtn = document.getElementById('playPauseBtn');
const playIcon = document.querySelector('.play-icon');
const pauseIcon = document.querySelector('.pause-icon');
const playerStatus = document.getElementById('playerStatus');
const formatToggleBtn = document.getElementById('formatToggleBtn');
const formatLabel = document.getElementById('formatLabel');
if (!playPauseBtn) return;
@@ -360,8 +362,19 @@ document.addEventListener('DOMContentLoaded', function () {
const streamFallback = playPauseBtn.getAttribute('data-stream-fallback');
const stationName = playPauseBtn.getAttribute('data-station-name');
// Stato formato audio (preferenza salvata in localStorage)
const storageKey = 'audioFormat_' + stationName;
let currentFormat = localStorage.getItem(storageKey) || 'hls'; // 'hls' o 'direct'
let isPlaying = false;
// Aggiorna label del formato
function updateFormatLabel() {
if (formatLabel) {
formatLabel.textContent = currentFormat === 'hls' ? 'HLS' : 'MP3';
}
}
updateFormatLabel();
// Configurazione HLS.js Ottimizzata per Qualità
const hlsConfig = {
debug: false,
@@ -379,7 +392,16 @@ document.addEventListener('DOMContentLoaded', function () {
* Logica di Selezione Player
* Priorità: HLS.js (PC/Android) > Nativo (iOS) > Fallback (Direct Stream)
*/
function initStream() {
function initStream(forceFormat = null) {
const preferredFormat = forceFormat || currentFormat;
// Se l'utente ha scelto formato diretto, usa subito MP3/AAC
if (preferredFormat === 'direct') {
console.log('User preference: Direct stream (MP3/AAC)');
useDirectStream();
return;
}
// STRATEGIA 1: HLS.js (Preferita per PC e Android)
if (Hls.isSupported()) {
console.log('Strategy: HLS.js (High Quality Control)');
@@ -427,7 +449,7 @@ document.addEventListener('DOMContentLoaded', function () {
hlsInstance.recoverMediaError();
break;
default:
fallbackToDirectStream();
useDirectStream();
break;
}
}
@@ -449,18 +471,54 @@ document.addEventListener('DOMContentLoaded', function () {
// STRATEGIA 3: Fallback (Stream MP3 diretto)
console.log('Strategy: Direct Fallback');
fallbackToDirectStream();
useDirectStream();
}
function fallbackToDirectStream() {
function useDirectStream() {
if (hlsInstance) {
hlsInstance.destroy();
hlsInstance = null;
}
console.log('Using direct stream:', streamFallback);
audioPlayer.src = streamFallback;
attemptPlay();
}
// Funzione per cambiare formato
function toggleFormat() {
const wasPlaying = !audioPlayer.paused;
// Cambia formato
currentFormat = currentFormat === 'hls' ? 'direct' : 'hls';
localStorage.setItem(storageKey, currentFormat);
console.log('Switching to format:', currentFormat);
// Aggiorna label
updateFormatLabel();
// Ferma riproduzione corrente
if (audioPlayer) {
audioPlayer.pause();
}
// Pulisci player precedente
if (hlsInstance) {
hlsInstance.destroy();
hlsInstance = null;
}
audioPlayer.src = '';
// Reinizializza con nuovo formato
initStream(currentFormat);
// Riprendi riproduzione se era in corso
if (wasPlaying) {
setTimeout(() => {
togglePlay();
}, 100);
}
}
function attemptPlay() {
// Nota: chiamare .play() senza interazione utente fallirà.
// Questa funzione prepara lo stato. Il vero play avviene al click.
@@ -519,10 +577,18 @@ document.addEventListener('DOMContentLoaded', function () {
updateUI(false);
};
// Format toggle button listener
if (formatToggleBtn) {
formatToggleBtn.onclick = (e) => {
e.preventDefault();
toggleFormat();
};
}
// Setup Media Session
setupMediaSession(stationName, playPauseBtn.getAttribute('data-station-slogan'), playPauseBtn.getAttribute('data-station-logo'), playIcon, pauseIcon);
console.log('Player System Ready.');
console.log('Player System Ready with format toggle.');
}
/**
@@ -705,7 +771,7 @@ document.addEventListener('DOMContentLoaded', function () {
* Setup modalità standalone (PWA)
*/
function setupStandaloneMode() {
const appContainer = document.querySelector('.container');
const appContainer = document.querySelector('.container') || document.querySelector('.container-fluid');
if (!appContainer) return;
if (window.opener && !window.opener.closed) {
@@ -748,7 +814,7 @@ document.addEventListener('DOMContentLoaded', function () {
setupOpenAppButton();
if (document.querySelector('.container')) {
if (document.querySelector('.container') || document.querySelector('.container-fluid')) {
setupStandaloneMode();
attachLinkListeners();
setupHistoryNavigation();