const playPauseBtn = document.getElementById('playPauseBtn'); const volumeControl = document.getElementById('volumeControl'); const muteBtn = document.getElementById('muteBtn'); const albumCover = document.getElementById('albumCover'); const songTitle = document.getElementById('songTitle'); const artistName = document.getElementById('artistName'); const lyricsBtn = document.getElementById('lyricsBtn'); const lyricsModal = new bootstrap.Modal(document.getElementById('lyricsModal')); const lyricsContent = document.getElementById('lyricsContent'); const historicoBtn = document.getElementById('historicoBtn'); const historicoModal = new bootstrap.Modal(document.getElementById('historicoModal')); const historicoContent = document.getElementById('historicoContent'); const sobreBtn = document.getElementById('sobreBtn'); const sobreModal = new bootstrap.Modal(document.getElementById('sobreModal')); const sobreContent = document.getElementById('sobreContent'); const progBtn = document.getElementById('progBtn'); const progModal = new bootstrap.Modal(document.getElementById('progModal')); const progContent = document.getElementById('progContent'); let audioElement = null; let isMuted = false; let previousVolume = 1; let currentSong = ''; let isFirstLoad = true; let lastAlbumCoverUrl = ''; function togglePlay() { if (!audioElement) { startNewStream(); } else if (audioElement.paused) { startNewStream(); } else { audioElement.pause(); audioElement.src = ''; audioElement = null; playPauseBtn.innerHTML = ''; } } function startNewStream() { if (audioElement) { audioElement.pause(); audioElement.src = ''; } audioElement = new Audio(); audioElement.src = streamUrl + '?nocache=' + new Date().getTime(); audioElement.volume = isMuted ? 0 : volumeControl.value / 100; audioElement.play(); playPauseBtn.innerHTML = ''; } function updateVolume() { if (audioElement) { audioElement.volume = volumeControl.value / 100; if (isMuted && volumeControl.value > 0) { toggleMute(); } } updateMuteButtonIcon(); } function toggleMute() { isMuted = !isMuted; if (audioElement) { if (isMuted) { previousVolume = audioElement.volume; audioElement.volume = 0; volumeControl.value = 0; } else { audioElement.volume = previousVolume; volumeControl.value = previousVolume * 100; } } updateMuteButtonIcon(); } function updateMuteButtonIcon() { if (isMuted || volumeControl.value == 0) { muteBtn.innerHTML = ''; } else if (volumeControl.value < 50) { muteBtn.innerHTML='' ; } else { muteBtn.innerHTML='' ; } } // Add this function to update the recent songs list function updateRecentSongs(artist, title, albumArt) { const newSong={ artist, title, albumArt }; recentSongs.unshift(newSong); if (recentSongs.length> MAX_RECENT_SONGS) { recentSongs.pop(); } displayRecentSongs(); } // Add this function to display the recent songs function displayRecentSongs() { const recentSongsList = document.getElementById('recentSongsList'); recentSongsList.innerHTML = ''; recentSongs.forEach(song => { const songItem = document.createElement('div'); songItem.className = 'recent-song-item'; songItem.innerHTML = ` ${song.title} - ${song.artist}
${song.artist}
${song.title}
`; recentSongsList.appendChild(songItem); }); } function setDefaultCover() { if (lastAlbumCoverUrl !== './img/albumart.jpg?v=1749046493') { lastAlbumCoverUrl = './img/albumart.jpg?v=1749046493'; fadeImage(lastAlbumCoverUrl); } } function fadeImage(newSrc) { albumCover.style.opacity = 0; setTimeout(() => { albumCover.src = newSrc; albumCover.style.opacity = 1; albumCover.onload = () => { updateBackgroundColor(); }; }, 300); } function showLyrics() { const artist = artistName.textContent; const title = songTitle.textContent; fetchLyrics(artist, title); lyricsModal.show(); } function showPedido() { pedidoModal.show(); } function showHistorico() { historicoModal.show(); } function showSobre() { sobreModal.show(); } function showProg() { progModal.show(); } async function fetchLyrics(artist, title) { lyricsContent.textContent = 'Buscando letra...'; try { // First search for the song const searchResponse = await fetch(`https://lrclib.net/api/search?track_name=${encodeURIComponent(title)}&artist_name=${encodeURIComponent(artist)}`); const searchResults = await searchResponse.json(); if (searchResults && searchResults.length > 0) { // Get the first result's ID const songId = searchResults[0].id; // Fetch the lyrics using the song ID const lyricsResponse = await fetch(`https://lrclib.net/api/get/${songId}`); const lyricsData = await lyricsResponse.json(); if (lyricsData && lyricsData.plainLyrics) { lyricsContent.innerHTML = lyricsData.plainLyrics.replace(/\n/g, '
'); } else { lyricsContent.textContent = 'Letra não encontrada para esta música.'; } } else { lyricsContent.textContent = 'Letra não encontrada para esta música.'; } } catch (error) { console.error('Error fetching lyrics:', error); lyricsContent.textContent = 'Não foi possível carregar a letra. Por favor, tente novamente mais tarde.'; } } updateMuteButtonIcon();