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();
const streamUrl = 'https://azura.servercast.com.br/listen/vale_fm/radio.mp3';
let audioElement = null;
let isMuted = false;
let previousVolume = 1;
let currentSong = '';
let isFirstLoad = true;
let lastAlbumCoverUrl = '';
let recentSongs = [];
const MAX_RECENT_SONGS = 7;
const lastfmApiKey = '';
playPauseBtn.addEventListener('click', togglePlay);
volumeControl.addEventListener('input', updateVolume);
muteBtn.addEventListener('click', toggleMute);
lyricsBtn.addEventListener('click', showLyrics);
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.artist}
${song.title}
`;
recentSongsList.appendChild(songItem);
});
}
function updateMetadata() {
$.ajax({
url: 'https://azura.servercast.com.br/api/nowplaying/73',
dataType: 'json',
success: function(data) {
if (data && data.listeners && data.listeners.current !== undefined) {
document.getElementById('listenerCount').textContent = data.listeners.current;
}
if (data && data.now_playing && data.now_playing.song) {
const newArtist = data.now_playing.song.artist;
const newTitle = data.now_playing.song.title;
if (currentSong !== `${newArtist} - ${newTitle}` || isFirstLoad) {
currentSong = `${newArtist} - ${newTitle}`;
artistName.textContent = newArtist;
songTitle.textContent = newTitle;
fetchAlbumCover(newArtist, newTitle);
isFirstLoad = false;
}
} else {
songTitle.textContent = 'Transmissão Online';
artistName.textContent = data.station.name || 'Rádio Desconhecida';
}
},
error: function(xhr, status, error) {
console.error('Error fetching metadata:', error);
songTitle.textContent = 'Não foi possível obter os dados';
artistName.textContent = 'Tente novamente';
document.getElementById('listenerCount').textContent = '-';
}
});
}
function fetchAlbumCover(artist, track) {
const searchTerm = encodeURIComponent(`${artist} - ${track}`);
const url = `https://itunes.apple.com/search?term=${searchTerm}&media=music&limit=1`;
$.ajax({
url: url,
dataType: 'jsonp',
success: function(data) {
if (data.results && data.results.length > 0) {
const artworkUrl = data.results[0].artworkUrl100.replace('100x100', '600x600');
if (artworkUrl !== lastAlbumCoverUrl) {
lastAlbumCoverUrl = artworkUrl;
fadeImage(artworkUrl);
updateRecentSongs(artist, track, artworkUrl);
}
} else {
setDefaultCover();
updateRecentSongs(artist, track, './img/logo.png?v=1746385891');
}
},
error: function(xhr, status, error) {
console.error('Error fetching iTunes data:', error);
setDefaultCover();
updateRecentSongs(artist, track, './img/logo.png?v=1746385891');
}
});
}
function setDefaultCover() {
if (lastAlbumCoverUrl !== './img/logo.png?v=1746385891') {
lastAlbumCoverUrl = './img/logo.png?v=1746385891';
fadeImage(lastAlbumCoverUrl);
}
}
function fadeImage(newSrc) {
albumCover.style.opacity = 0;
setTimeout(() => {
albumCover.src = newSrc;
albumCover.style.opacity = 1;
albumCover.onload = () => {
updateBackgroundColor();
};
}, 300);
}
function updateBackgroundColor() {
if (albumCover.complete && albumCover.naturalHeight !== 0) {
try {
const dominantColor = colorThief.getColor(albumCover);
const [r, g, b] = dominantColor;
document.body.style.backgroundColor = `rgba(${r}, ${g}, ${b}, 0.9)`;
} catch (error) {
console.error('Error getting dominant color:', error);
document.body.style.backgroundColor = 'rgba(240, 0, 0, 0.9)';
}
} else {
console.error('Image not fully loaded');
document.body.style.backgroundColor = 'rgba(240, 0, 0, 0.9)';
}
}
albumCover.addEventListener('load', updateBackgroundColor);
albumCover.addEventListener('error', () => {
console.error('Error loading image');
document.body.style.backgroundColor = 'rgba(240, 0, 0, 0.9)';
});
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.';
}
}
updateMetadata();
setInterval(updateMetadata, 25000);
updateMuteButtonIcon();
// Background animation
const canvas = document.getElementById('backgroundCanvas');
const ctx = canvas.getContext('2d');
// Set canvas size
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// Ball class
class Ball {
constructor() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.size = Math.random() * 5 + 1;
this.speedX = Math.random() * 1 - 0.5;
this.speedY = Math.random() * 1 - 0.5;
}
update() {
this.x += this.speedX;
this.y += this.speedY;
if (this.x > canvas.width || this.x < 0) { this.speedX=-this.speedX; } if (this.y> canvas.height || this.y < 0) {
this.speedY=-this.speedY; } } draw() { ctx.fillStyle='rgba(255, 255, 255, 0.5)' ; ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2); ctx.closePath(); ctx.fill(); } } const balls=[]; for
(let i=0; i < 50; i++) { balls.push(new Ball()); } function animate() { ctx.clearRect(0, 0, canvas.width,
canvas.height); balls.forEach(ball=> {
ball.update();
ball.draw();
});
requestAnimationFrame(animate);
}
animate();
document.addEventListener('DOMContentLoaded', (event) => {
const videoModal = new bootstrap.Modal(document.getElementById('videoModal'));
const videoContainer = document.getElementById('video-container');
const videoLink = 'https://www.youtube.com/embed/ymA49CZjmHg?si=PgsLYu5aRA5PfseV';
// Mostrar o modal após xx segundos
setTimeout(() => {
videoContainer.innerHTML = ``;
videoModal.show();
}, 2000);
// Limpar o vídeo ao fechar o modal
document.getElementById('videoModal').addEventListener('hidden.bs.modal', () => {
videoContainer.innerHTML = '';
});
});