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 colorThief = new ColorThief();
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='' ; } }
function setDefaultCover() {
if (lastAlbumCoverUrl !=='./img/logo.png?v=1776913295' ) {
lastAlbumCoverUrl='./img/logo.png?v=1776913295' ;
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();
}
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();