170 lines
5.1 KiB
JavaScript
170 lines
5.1 KiB
JavaScript
// Volume adjustment for audio elements
|
|
document.querySelectorAll("audio").forEach((audio) => {
|
|
audio.volume = 0.5; // Volume range is from 0.0 (silent) to 1.0 (maximum)
|
|
});
|
|
|
|
// Shuffle Playlist (Fisher-Yates)
|
|
function fisherYatesShuffle(songs) {
|
|
for (let i = songs.length - 1; i > 0; i--) {
|
|
const j = Math.floor(Math.random() * (i + 1));
|
|
[songs[i], songs[j]] = [songs[j], songs[i]];
|
|
}
|
|
return songs;
|
|
}
|
|
|
|
// Load the JSON file containing music data directly from chat.veltron.net
|
|
async function loadMusicData() {
|
|
const targetUrl = 'https://api.veltron.net'; // Directly use the target URL
|
|
const response = await fetch(targetUrl);
|
|
|
|
// Check if the request was successful
|
|
if (!response.ok) {
|
|
console.error("Failed to fetch music data:", response.statusText);
|
|
return [];
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
// Get the "frutigerAeroBliss" section from the JSON
|
|
const section = data.ols.find(item => item["@id"] === "frutigerAeroBliss");
|
|
|
|
if (!section) {
|
|
console.error('Section "frutigerAeroBliss" not found.');
|
|
return [];
|
|
}
|
|
|
|
// Map the songs into a simplified format with direct audio URLs
|
|
const baseUrl = "https://cdn.veltron.net/aero/music/"; // Base URL for the music files
|
|
const playlist = section.li.map(item => ({
|
|
title: item.a.title,
|
|
url: baseUrl + item.a["@data-src-mp3"].split("/").pop(), // Build the full URL from the base path
|
|
background: item.a["@data-background"] || '', // Optional background if available
|
|
artist: item.a["@data-artist"] || '',
|
|
spotifyLink: item.a["@data-spotify"] || '',
|
|
youtubeLink: item.a["@data-youtube"] || ''
|
|
}));
|
|
|
|
// Shuffle the playlist
|
|
return fisherYatesShuffle(playlist);
|
|
}
|
|
|
|
// Define Music Player
|
|
const NAV_MUSIC = document.getElementById("navMusic");
|
|
const BUTTON_PLAY = document.getElementById("navMusicPlay");
|
|
const BUTTON_PAUSE = document.getElementById("navMusicPause");
|
|
const BUTTON_PREVIOUS = document.getElementById("navMusicPrevious");
|
|
const BUTTON_NEXT = document.getElementById("navMusicNext");
|
|
const CURRENT_SONG_INFO = document.getElementById("currentSongInfo");
|
|
|
|
let songs = [];
|
|
let currentIndex = 0;
|
|
let currentTime = 0;
|
|
let isPlaying = false; // Track play/pause state
|
|
|
|
// Save state to sessionStorage
|
|
function saveState() {
|
|
sessionStorage.setItem("isPlaying", isPlaying);
|
|
}
|
|
|
|
// Load state from sessionStorage
|
|
function loadState() {
|
|
const savedIsPlaying = sessionStorage.getItem("isPlaying");
|
|
|
|
if (savedIsPlaying !== null) {
|
|
isPlaying = savedIsPlaying === "true";
|
|
}
|
|
}
|
|
|
|
// Load the current song
|
|
function loadSong(index) {
|
|
if (index < 0 || index >= songs.length) return;
|
|
NAV_MUSIC.src = songs[index].url; // Directly set the audio URL (no proxy needed)
|
|
CURRENT_SONG_INFO.innerText = `Featured song: ${songs[index].title}`;
|
|
NAV_MUSIC.load();
|
|
}
|
|
|
|
// Update button states based on `isPlaying`
|
|
function updateButtonStates() {
|
|
if (isPlaying) {
|
|
BUTTON_PLAY.classList.add("active");
|
|
BUTTON_PAUSE.classList.remove("active");
|
|
} else {
|
|
BUTTON_PAUSE.classList.add("active");
|
|
BUTTON_PLAY.classList.remove("active");
|
|
}
|
|
}
|
|
|
|
// Event listeners for playback controls
|
|
BUTTON_PLAY.addEventListener("click", function () {
|
|
isPlaying = true;
|
|
NAV_MUSIC.play();
|
|
updateButtonStates();
|
|
saveState();
|
|
});
|
|
|
|
BUTTON_PAUSE.addEventListener("click", function () {
|
|
isPlaying = false;
|
|
NAV_MUSIC.pause();
|
|
updateButtonStates();
|
|
saveState();
|
|
});
|
|
|
|
BUTTON_PREVIOUS.addEventListener("click", function () {
|
|
currentIndex = currentIndex === 0 ? songs.length - 1 : currentIndex - 1;
|
|
currentTime = 0; // Reset time when switching songs
|
|
loadSong(currentIndex);
|
|
if (isPlaying) {
|
|
NAV_MUSIC.play();
|
|
}
|
|
updateButtonStates();
|
|
saveState();
|
|
});
|
|
|
|
BUTTON_NEXT.addEventListener("click", function () {
|
|
currentIndex = (currentIndex + 1) % songs.length;
|
|
currentTime = 0; // Reset time when switching songs
|
|
loadSong(currentIndex);
|
|
if (isPlaying) {
|
|
NAV_MUSIC.play();
|
|
}
|
|
updateButtonStates();
|
|
saveState();
|
|
});
|
|
|
|
// Autoplay next song when current ends
|
|
NAV_MUSIC.addEventListener("ended", function () {
|
|
currentIndex = (currentIndex + 1) % songs.length;
|
|
currentTime = 0; // Reset time when a new song starts
|
|
loadSong(currentIndex);
|
|
if (isPlaying) {
|
|
NAV_MUSIC.play();
|
|
}
|
|
saveState();
|
|
});
|
|
|
|
// Initial load
|
|
window.addEventListener("load", async () => {
|
|
songs = await loadMusicData();
|
|
if (songs.length > 0) {
|
|
loadState();
|
|
loadSong(currentIndex);
|
|
if (isPlaying) {
|
|
NAV_MUSIC.play();
|
|
}
|
|
updateButtonStates();
|
|
}
|
|
});
|
|
|
|
// Pause all audio if user presses play on another audio element
|
|
document.addEventListener(
|
|
"play",
|
|
function (e) {
|
|
var audios = document.getElementsByTagName("audio");
|
|
for (var i = 0, len = audios.length; i < len; i++) {
|
|
if (audios[i] != e.target) {
|
|
audios[i].pause();
|
|
}
|
|
}
|
|
},
|
|
true
|
|
);
|