/* ========================================== Spotify Web Player Interactive Logic (app.js) ========================================== */ document.addEventListener('DOMContentLoaded', () => { // --- Audio State & Track Inventory --- let isPlaying = false; let isLiked = false; let currentTime = 0; let duration = 225; // 3:45 in seconds let timerInterval = null; let currentVolume = 0.7; let isMuted = false; // Track Playlist Data const tracks = [ { title: 'Resonance', artist: 'HOME', cover: 'https://images.unsplash.com/photo-1470225620780-dba8ba36b745?w=400&auto=format&fit=crop&q=80', duration: 212 }, { title: 'Midnight City', artist: 'M83', cover: 'https://images.unsplash.com/photo-1514525253161-7a46d19cd819?w=400&auto=format&fit=crop&q=80', duration: 243 }, { title: 'Coffee & Chill', artist: 'Lo-Fi Girl', cover: 'https://images.unsplash.com/photo-1511671782779-c97d3d27a1d4?w=400&auto=format&fit=crop&q=80', duration: 180 }, { title: 'Starboy', artist: 'The Weeknd', cover: 'https://images.unsplash.com/photo-1508700115892-45ecd05ae2ad?w=400&auto=format&fit=crop&q=80', duration: 230 }, { title: 'Chill Jazz Vibes', artist: 'Miles Davis', cover: 'https://images.unsplash.com/photo-1493225457124-a3eb161ffa5f?w=400&auto=format&fit=crop&q=80', duration: 310 }, { title: 'AI 前沿趨勢解密', artist: 'Tech Daily', cover: 'https://images.unsplash.com/photo-1590602847861-f357a9332bbc?w=400&auto=format&fit=crop&q=80', duration: 2880 } ]; let currentTrackIndex = 0; // --- Web Audio Synthesizer (Realistic Sound Feedback) --- let audioCtx = null; let synthOscillator = null; let gainNode = null; function initSynthAudio() { if (!audioCtx) { const AudioContext = window.AudioContext || window.webkitAudioContext; audioCtx = new AudioContext(); } } function playToneSynth() { if (!audioCtx) return; try { if (synthOscillator) synthOscillator.stop(); synthOscillator = audioCtx.createOscillator(); gainNode = audioCtx.createGain(); synthOscillator.type = 'sine'; // Harmonic chord tone based on track index const baseFreq = 220 + (currentTrackIndex * 40); synthOscillator.frequency.setValueAtTime(baseFreq, audioCtx.currentTime); gainNode.gain.setValueAtTime(0.05 * (isMuted ? 0 : currentVolume), audioCtx.currentTime); gainNode.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + 1.5); synthOscillator.connect(gainNode); gainNode.connect(audioCtx.destination); synthOscillator.start(); synthOscillator.stop(audioCtx.currentTime + 1.5); } catch (e) { console.log('Audio Context notice:', e); } } // --- DOM Elements --- const btnPlayToggle = document.getElementById('btn-play-toggle'); const iconPlay = document.getElementById('icon-play'); const iconPause = document.getElementById('icon-pause'); const playerThumb = document.getElementById('player-thumb'); const playerTitle = document.getElementById('player-title'); const playerArtist = document.getElementById('player-artist'); const currentTimeEl = document.getElementById('current-time'); const totalTimeEl = document.getElementById('total-time'); const progressBarFill = document.getElementById('progress-bar-fill'); const progressThumb = document.getElementById('progress-thumb'); const progressBarWrapper = document.getElementById('progress-bar-wrapper'); const playerLikeBtn = document.getElementById('player-like-btn'); const btnPrev = document.getElementById('btn-prev'); const btnNext = document.getElementById('btn-next'); const btnShuffle = document.getElementById('btn-shuffle'); const btnRepeat = document.getElementById('btn-repeat'); const searchInput = document.getElementById('main-search-input'); const filterPills = document.querySelectorAll('.filter-pills-bar .btn-dark-pill'); const volumeBarWrapper = document.getElementById('volume-bar-wrapper'); const volumeBarFill = document.getElementById('volume-bar-fill'); const btnVolumeIcon = document.getElementById('btn-volume-icon'); // --- Helper Functions --- function formatTime(seconds) { const min = Math.floor(seconds / 60); const sec = Math.floor(seconds % 60); return `${min}:${sec < 10 ? '0' : ''}${sec}`; } function updatePlayerUI() { const track = tracks[currentTrackIndex]; playerThumb.src = track.cover; playerTitle.textContent = track.title; playerArtist.textContent = track.artist; duration = track.duration; totalTimeEl.textContent = formatTime(duration); // Reset progress currentTime = 0; updateProgressUI(); } function updateProgressUI() { currentTimeEl.textContent = formatTime(currentTime); const pct = (currentTime / duration) * 100; progressBarFill.style.width = `${pct}%`; progressThumb.style.left = `${pct}%`; } function startPlayback() { initSynthAudio(); isPlaying = true; iconPlay.style.display = 'none'; iconPause.style.display = 'block'; playToneSynth(); clearInterval(timerInterval); timerInterval = setInterval(() => { if (currentTime < duration) { currentTime += 1; updateProgressUI(); } else { nextTrack(); } }, 1000); } function pausePlayback() { isPlaying = false; iconPlay.style.display = 'block'; iconPause.style.display = 'none'; clearInterval(timerInterval); } function togglePlay() { if (isPlaying) { pausePlayback(); } else { startPlayback(); } } function nextTrack() { currentTrackIndex = (currentTrackIndex + 1) % tracks.length; updatePlayerUI(); if (isPlaying) startPlayback(); } function prevTrack() { currentTrackIndex = (currentTrackIndex - 1 + tracks.length) % tracks.length; updatePlayerUI(); if (isPlaying) startPlayback(); } // --- Play/Pause Toggle Listener --- btnPlayToggle.addEventListener('click', togglePlay); btnNext.addEventListener('click', nextTrack); btnPrev.addEventListener('click', prevTrack); // --- Interactive Seek Bar --- progressBarWrapper.addEventListener('click', (e) => { const rect = progressBarWrapper.getBoundingClientRect(); const clickX = e.clientX - rect.left; const width = rect.width; const clickPct = Math.max(0, Math.min(1, clickX / width)); currentTime = Math.floor(clickPct * duration); updateProgressUI(); }); // --- Like Button Toggle --- playerLikeBtn.addEventListener('click', () => { isLiked = !isLiked; if (isLiked) { playerLikeBtn.classList.add('liked'); } else { playerLikeBtn.classList.remove('liked'); } }); // --- Card & Quick Grid Play Click Event --- const allCards = document.querySelectorAll('.spotify-card, .quick-card'); allCards.forEach((card) => { card.addEventListener('click', (e) => { const trackTitle = card.getAttribute('data-track'); const trackArtist = card.getAttribute('data-artist'); const trackCover = card.getAttribute('data-cover'); if (trackTitle && trackArtist) { // Find or create track const existingIdx = tracks.findIndex(t => t.title === trackTitle); if (existingIdx !== -1) { currentTrackIndex = existingIdx; } else { tracks.push({ title: trackTitle, artist: trackArtist, cover: trackCover || tracks[0].cover, duration: 215 }); currentTrackIndex = tracks.length - 1; } updatePlayerUI(); startPlayback(); } }); }); // --- Real-Time Search Filter --- if (searchInput) { searchInput.addEventListener('input', (e) => { const query = e.target.value.toLowerCase().trim(); document.querySelectorAll('.spotify-card').forEach(card => { const title = card.querySelector('.card-title')?.textContent.toLowerCase() || ''; const desc = card.querySelector('.card-desc')?.textContent.toLowerCase() || ''; if (title.includes(query) || desc.includes(query)) { card.style.display = 'flex'; } else { card.style.display = 'none'; } }); document.querySelectorAll('.quick-card').forEach(card => { const title = card.querySelector('.quick-card-title')?.textContent.toLowerCase() || ''; if (title.includes(query)) { card.style.display = 'flex'; } else { card.style.display = 'none'; } }); }); } // --- Category Pills Filtering --- filterPills.forEach(pill => { pill.addEventListener('click', () => { filterPills.forEach(p => p.classList.remove('active')); pill.classList.add('active'); const filterCategory = pill.getAttribute('data-filter'); const sections = document.querySelectorAll('.card-section'); sections.forEach(sec => { const cat = sec.getAttribute('data-category'); if (filterCategory === 'all' || cat === filterCategory) { sec.style.display = 'block'; } else { sec.style.display = 'none'; } }); }); }); // --- Volume Slider & Mute Toggle --- if (volumeBarWrapper) { volumeBarWrapper.addEventListener('click', (e) => { const rect = volumeBarWrapper.getBoundingClientRect(); const clickX = e.clientX - rect.left; const pct = Math.max(0, Math.min(1, clickX / rect.width)); currentVolume = pct; isMuted = false; volumeBarFill.style.width = `${pct * 100}%`; }); } if (btnVolumeIcon) { btnVolumeIcon.addEventListener('click', () => { isMuted = !isMuted; if (isMuted) { volumeBarFill.style.width = '0%'; } else { volumeBarFill.style.width = `${currentVolume * 100}%`; } }); } // --- Shuffle & Repeat Toggle Buttons --- if (btnShuffle) { btnShuffle.addEventListener('click', () => { btnShuffle.classList.toggle('active'); }); } if (btnRepeat) { btnRepeat.addEventListener('click', () => { btnRepeat.classList.toggle('active'); }); } // Initial Load Setup updatePlayerUI(); });