PORTFOLIO
L’esperienza che dà voce al tuo brand

*/let currentAudio = null;function controlAudio(btn) { const track = btn.closest('.luxury-track'); if (!track) return;const audio = track.querySelector('audio'); const progressBar = track.querySelector('.luxury-progress-bar'); const playIcon = btn.querySelector('.icon-play'); const pauseIcon = btn.querySelector('.icon-pause');if (!audio) return;// Ferma l'audio corrente se diverso if (currentAudio && currentAudio !== audio) { try { currentAudio.pause(); } catch (e) {} const otherTrack = currentAudio.closest('.luxury-track'); if (otherTrack) { const otherPlay = otherTrack.querySelector('.icon-play'); const otherPause = otherTrack.querySelector('.icon-pause'); if (otherPlay) otherPlay.style.setProperty('display', 'block', 'important'); if (otherPause) otherPause.style.setProperty('display', 'none', 'important'); const otherProgress = otherTrack.querySelector('.luxury-progress-bar'); if (otherProgress) otherProgress.style.width = '0%'; } currentAudio = null; }// Carica la sorgente SOLO al primo click (supporta data-src) if (!audio.src || audio.src.trim() === '') { const src = audio.dataset && audio.dataset.src ? audio.dataset.src : null; if (src) audio.src = src; }// Play / Pause con gestione della promise if (audio.paused) { const playPromise = audio.play(); if (playPromise !== undefined) { playPromise.then(() => { if (playIcon) playIcon.style.setProperty('display', 'none', 'important'); if (pauseIcon) pauseIcon.style.setProperty('display', 'block', 'important'); currentAudio = audio; }).catch(() => { // Play bloccato (es. autoplay policy): mantieni stato play if (playIcon) playIcon.style.setProperty('display', 'block', 'important'); if (pauseIcon) pauseIcon.style.setProperty('display', 'none', 'important'); }); } else { if (playIcon) playIcon.style.setProperty('display', 'none', 'important'); if (pauseIcon) pauseIcon.style.setProperty('display', 'block', 'important'); currentAudio = audio; } } else { audio.pause(); if (playIcon) playIcon.style.setProperty('display', 'block', 'important'); if (pauseIcon) pauseIcon.style.setProperty('display', 'none', 'important'); currentAudio = null; }// Aggiungo listener di progresso/ended una sola volta if (!audio._luxuryListenersAdded) { audio._luxuryListenersAdded = true;audio.addEventListener('timeupdate', () => { if (!audio.duration || isNaN(audio.duration)) return; const percent = (audio.currentTime / audio.duration) * 100; if (progressBar) progressBar.style.width = percent + '%'; });audio.addEventListener('pause', () => { // Se preferisci resettare la barra alla pausa, decommenta la riga sotto // if (progressBar) progressBar.style.width = '0%'; });audio.addEventListener('ended', () => { if (playIcon) playIcon.style.setProperty('display', 'block', 'important'); if (pauseIcon) pauseIcon.style.setProperty('display', 'none', 'important'); if (progressBar) progressBar.style.width = '0%'; if (currentAudio === audio) currentAudio = null; });audio.addEventListener('error', () => { if (playIcon) playIcon.style.setProperty('display', 'block', 'important'); if (pauseIcon) pauseIcon.style.setProperty('display', 'none', 'important'); if (progressBar) progressBar.style.width = '0%'; if (currentAudio === audio) currentAudio = null; }); } }