Spaces:
Running
Running
Manual changes saved
Browse files
script.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
| 1 |
-
// Simple MySpace-style music player
|
| 2 |
-
// Fresh reset – ignores any old visualizer code
|
| 3 |
-
|
| 4 |
|
|
|
|
| 5 |
// === PLAYLIST ===
|
| 6 |
-
// You can add more songs later if you want.
|
| 7 |
const playlist = [
|
| 8 |
{
|
| 9 |
title: "The Ocean",
|
|
@@ -20,24 +18,48 @@
|
|
| 20 |
const prevBtn = document.getElementById("prev-btn");
|
| 21 |
const nextBtn = document.getElementById("next-btn");
|
| 22 |
const seekBar = document.getElementById("seek-bar");
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
//if
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
let currentTrackIndex = 0;
|
| 39 |
let isPlaying = false;
|
| 40 |
let isSeeking = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
// === CORE FUNCTIONS ===
|
| 43 |
|
|
@@ -51,21 +73,31 @@
|
|
| 51 |
}
|
| 52 |
|
| 53 |
function playTrack() {
|
| 54 |
-
audioEl
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
}
|
| 64 |
|
| 65 |
function pauseTrack() {
|
| 66 |
audioEl.pause();
|
| 67 |
isPlaying = false;
|
| 68 |
playPauseBtn.textContent = "▶"; // play icon
|
|
|
|
| 69 |
}
|
| 70 |
|
| 71 |
function togglePlay() {
|
|
@@ -92,18 +124,11 @@
|
|
| 92 |
// === EVENT LISTENERS ===
|
| 93 |
|
| 94 |
// Play / Pause button
|
| 95 |
-
playPauseBtn.addEventListener("click",
|
| 96 |
-
togglePlay();
|
| 97 |
-
});
|
| 98 |
|
| 99 |
// Next / Previous
|
| 100 |
-
nextBtn.addEventListener("click",
|
| 101 |
-
|
| 102 |
-
});
|
| 103 |
-
|
| 104 |
-
prevBtn.addEventListener("click", () => {
|
| 105 |
-
playPrev();
|
| 106 |
-
});
|
| 107 |
|
| 108 |
// Keep seek bar in sync with audio
|
| 109 |
audioEl.addEventListener("timeupdate", () => {
|
|
@@ -129,10 +154,16 @@
|
|
| 129 |
playNext();
|
| 130 |
});
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
// === INITIALIZE ===
|
| 133 |
|
| 134 |
// Load first track
|
| 135 |
loadTrack(currentTrackIndex);
|
| 136 |
|
| 137 |
-
// Try autoplay on load (browser may block this until user
|
| 138 |
playTrack();
|
|
|
|
|
|
| 1 |
+
// Simple MySpace-style music player with fake EQ bars
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
document.addEventListener("DOMContentLoaded", () => {
|
| 4 |
// === PLAYLIST ===
|
|
|
|
| 5 |
const playlist = [
|
| 6 |
{
|
| 7 |
title: "The Ocean",
|
|
|
|
| 18 |
const prevBtn = document.getElementById("prev-btn");
|
| 19 |
const nextBtn = document.getElementById("next-btn");
|
| 20 |
const seekBar = document.getElementById("seek-bar");
|
| 21 |
+
const eqBars = document.querySelectorAll(".eq-visualizer .eq-bar");
|
| 22 |
+
|
| 23 |
+
// Safety check – if something's missing, don't crash
|
| 24 |
+
if (
|
| 25 |
+
!audioEl ||
|
| 26 |
+
!titleEl ||
|
| 27 |
+
!artistEl ||
|
| 28 |
+
!playPauseBtn ||
|
| 29 |
+
!prevBtn ||
|
| 30 |
+
!nextBtn ||
|
| 31 |
+
!seekBar
|
| 32 |
+
) {
|
| 33 |
+
console.warn("Music player: required elements not found.");
|
| 34 |
+
return;
|
| 35 |
+
}
|
| 36 |
|
| 37 |
let currentTrackIndex = 0;
|
| 38 |
let isPlaying = false;
|
| 39 |
let isSeeking = false;
|
| 40 |
+
let eqInterval = null;
|
| 41 |
+
|
| 42 |
+
// === EQ BAR ANIMATION (FAKE VISUALIZER) ===
|
| 43 |
+
|
| 44 |
+
function startEq() {
|
| 45 |
+
if (!eqBars.length || eqInterval) return;
|
| 46 |
+
eqInterval = setInterval(() => {
|
| 47 |
+
eqBars.forEach((bar) => {
|
| 48 |
+
const height = 20 + Math.random() * 80; // 20%–100%
|
| 49 |
+
bar.style.height = `${height}%`;
|
| 50 |
+
});
|
| 51 |
+
}, 120);
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
function stopEq() {
|
| 55 |
+
if (eqInterval) {
|
| 56 |
+
clearInterval(eqInterval);
|
| 57 |
+
eqInterval = null;
|
| 58 |
+
}
|
| 59 |
+
eqBars.forEach((bar) => {
|
| 60 |
+
bar.style.height = "25%"; // idle height
|
| 61 |
+
});
|
| 62 |
+
}
|
| 63 |
|
| 64 |
// === CORE FUNCTIONS ===
|
| 65 |
|
|
|
|
| 73 |
}
|
| 74 |
|
| 75 |
function playTrack() {
|
| 76 |
+
if (!audioEl.src) {
|
| 77 |
+
// just in case loadTrack didn't run for some reason
|
| 78 |
+
loadTrack(currentTrackIndex);
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
const playPromise = audioEl.play();
|
| 82 |
+
|
| 83 |
+
if (playPromise !== undefined) {
|
| 84 |
+
playPromise
|
| 85 |
+
.then(() => {
|
| 86 |
+
isPlaying = true;
|
| 87 |
+
playPauseBtn.textContent = "⏸"; // pause icon
|
| 88 |
+
startEq();
|
| 89 |
+
})
|
| 90 |
+
.catch((err) => {
|
| 91 |
+
console.warn("Autoplay/play blocked or failed:", err);
|
| 92 |
+
});
|
| 93 |
+
}
|
| 94 |
}
|
| 95 |
|
| 96 |
function pauseTrack() {
|
| 97 |
audioEl.pause();
|
| 98 |
isPlaying = false;
|
| 99 |
playPauseBtn.textContent = "▶"; // play icon
|
| 100 |
+
stopEq();
|
| 101 |
}
|
| 102 |
|
| 103 |
function togglePlay() {
|
|
|
|
| 124 |
// === EVENT LISTENERS ===
|
| 125 |
|
| 126 |
// Play / Pause button
|
| 127 |
+
playPauseBtn.addEventListener("click", togglePlay);
|
|
|
|
|
|
|
| 128 |
|
| 129 |
// Next / Previous
|
| 130 |
+
nextBtn.addEventListener("click", playNext);
|
| 131 |
+
prevBtn.addEventListener("click", playPrev);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
// Keep seek bar in sync with audio
|
| 134 |
audioEl.addEventListener("timeupdate", () => {
|
|
|
|
| 154 |
playNext();
|
| 155 |
});
|
| 156 |
|
| 157 |
+
// If there's an audio error (bad URL, etc.)
|
| 158 |
+
audioEl.addEventListener("error", (e) => {
|
| 159 |
+
console.warn("Audio element error:", e);
|
| 160 |
+
});
|
| 161 |
+
|
| 162 |
// === INITIALIZE ===
|
| 163 |
|
| 164 |
// Load first track
|
| 165 |
loadTrack(currentTrackIndex);
|
| 166 |
|
| 167 |
+
// Try autoplay on load (browser may block this until user clicks)
|
| 168 |
playTrack();
|
| 169 |
+
});
|