Fixes
This commit is contained in:
parent
d16493c7df
commit
c26ee66486
1 changed files with 56 additions and 31 deletions
|
|
@ -1,6 +1,6 @@
|
|||
// 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)
|
||||
audio.volume = 0.5;
|
||||
});
|
||||
|
||||
// Shuffle Playlist (Fisher-Yates)
|
||||
|
|
@ -14,10 +14,9 @@ function fisherYatesShuffle(songs) {
|
|||
|
||||
// Load the JSON file containing music data directly from chat.veltron.net
|
||||
async function loadMusicData() {
|
||||
const targetUrl = 'https://api.veltron.net/music'; // Directly use the target URL
|
||||
const targetUrl = "https://api.veltron.net/music";
|
||||
const response = await fetch(targetUrl);
|
||||
|
||||
// Check if the request was successful
|
||||
if (!response.ok) {
|
||||
console.error("Failed to fetch music data:", response.statusText);
|
||||
return [];
|
||||
|
|
@ -26,25 +25,37 @@ async function loadMusicData() {
|
|||
const data = await response.json();
|
||||
|
||||
// Get the "frutigerAeroBliss" section from the JSON
|
||||
const section = data.ols.find(item => item["@id"] === "frutigerAeroBliss");
|
||||
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://frutigeraeroarchive.org/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"] || ''
|
||||
}));
|
||||
// Base domain only — preserve subfolders from data-src-mp3
|
||||
const baseUrl = "https://frutigeraeroarchive.org/";
|
||||
|
||||
const playlist = section.li.map((item) => {
|
||||
const rawPath = item.a["@data-src-mp3"] || "";
|
||||
|
||||
// Remove leading slash if present, preserve nested folders like music/maplememory/song.mp3
|
||||
const normalizedPath = rawPath.replace(/^\/+/, "");
|
||||
|
||||
return {
|
||||
title: item.a.title || "Unknown Title",
|
||||
|
||||
// Full path keeps subfolders instead of only filename
|
||||
url: baseUrl + normalizedPath,
|
||||
|
||||
background: item.a["@data-background"] || "",
|
||||
artist: item.a["@data-artist"] || "",
|
||||
spotifyLink: item.a["@data-spotify"] || "",
|
||||
youtubeLink: item.a["@data-youtube"] || "",
|
||||
};
|
||||
});
|
||||
|
||||
// Shuffle the playlist
|
||||
return fisherYatesShuffle(playlist);
|
||||
}
|
||||
|
||||
|
|
@ -59,14 +70,14 @@ const CURRENT_SONG_INFO = document.getElementById("currentSongInfo");
|
|||
let songs = [];
|
||||
let currentIndex = 0;
|
||||
let currentTime = 0;
|
||||
let isPlaying = false; // Track play/pause state
|
||||
let isPlaying = false;
|
||||
|
||||
// Save state to sessionStorage
|
||||
// Save state
|
||||
function saveState() {
|
||||
sessionStorage.setItem("isPlaying", isPlaying);
|
||||
}
|
||||
|
||||
// Load state from sessionStorage
|
||||
// Load state
|
||||
function loadState() {
|
||||
const savedIsPlaying = sessionStorage.getItem("isPlaying");
|
||||
|
||||
|
|
@ -75,15 +86,16 @@ function loadState() {
|
|||
}
|
||||
}
|
||||
|
||||
// Load the current song
|
||||
// Load 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)
|
||||
|
||||
NAV_MUSIC.src = songs[index].url;
|
||||
CURRENT_SONG_INFO.innerText = `Featured song: ${songs[index].title}`;
|
||||
NAV_MUSIC.load();
|
||||
}
|
||||
|
||||
// Update button states based on `isPlaying`
|
||||
// Update buttons
|
||||
function updateButtonStates() {
|
||||
if (isPlaying) {
|
||||
BUTTON_PLAY.classList.add("active");
|
||||
|
|
@ -94,7 +106,7 @@ function updateButtonStates() {
|
|||
}
|
||||
}
|
||||
|
||||
// Event listeners for playback controls
|
||||
// Play
|
||||
BUTTON_PLAY.addEventListener("click", function () {
|
||||
isPlaying = true;
|
||||
NAV_MUSIC.play();
|
||||
|
|
@ -102,6 +114,7 @@ BUTTON_PLAY.addEventListener("click", function () {
|
|||
saveState();
|
||||
});
|
||||
|
||||
// Pause
|
||||
BUTTON_PAUSE.addEventListener("click", function () {
|
||||
isPlaying = false;
|
||||
NAV_MUSIC.pause();
|
||||
|
|
@ -109,59 +122,71 @@ BUTTON_PAUSE.addEventListener("click", function () {
|
|||
saveState();
|
||||
});
|
||||
|
||||
// Previous
|
||||
BUTTON_PREVIOUS.addEventListener("click", function () {
|
||||
currentIndex = currentIndex === 0 ? songs.length - 1 : currentIndex - 1;
|
||||
currentTime = 0; // Reset time when switching songs
|
||||
currentTime = 0;
|
||||
loadSong(currentIndex);
|
||||
|
||||
if (isPlaying) {
|
||||
NAV_MUSIC.play();
|
||||
}
|
||||
|
||||
updateButtonStates();
|
||||
saveState();
|
||||
});
|
||||
|
||||
// Next
|
||||
BUTTON_NEXT.addEventListener("click", function () {
|
||||
currentIndex = (currentIndex + 1) % songs.length;
|
||||
currentTime = 0; // Reset time when switching songs
|
||||
currentTime = 0;
|
||||
loadSong(currentIndex);
|
||||
|
||||
if (isPlaying) {
|
||||
NAV_MUSIC.play();
|
||||
}
|
||||
|
||||
updateButtonStates();
|
||||
saveState();
|
||||
});
|
||||
|
||||
// Autoplay next song when current ends
|
||||
// Autoplay next
|
||||
NAV_MUSIC.addEventListener("ended", function () {
|
||||
currentIndex = (currentIndex + 1) % songs.length;
|
||||
currentTime = 0; // Reset time when a new song starts
|
||||
currentTime = 0;
|
||||
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
|
||||
// Pause all other audio elements
|
||||
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) {
|
||||
const audios = document.getElementsByTagName("audio");
|
||||
|
||||
for (let i = 0; i < audios.length; i++) {
|
||||
if (audios[i] !== e.target) {
|
||||
audios[i].pause();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue