const canciones = [
{
titulo: "Korn - Twister Transistor",
archivo: "https://cs1.mp3.pm/listen/239685/aGdCU
TVSb2s5eGlDR20vVlpLYWhLclJrTTY4dURvZnB2L1E2ODVSWmlka1VQe
FpEOWlnRnRQNVpRQ1RLUDB1bHdQeWVqSVE4NkNGcDhzMXdtdU5YSE1LT
3ZLSE1MbVR6bGlEZmpNSXVKRlA1NENNT0RxWkFiYzBVdGpNRURocE4/K
orn_-_Twisted_Transistor_(mp3.pm).mp3",
portada: "https://encrypted-tbn0.gstatic.com/image
s?q=tbn:ANd9GcQXsUGDmKOVhhy1XU_qH5Mzxgxe2H2Q7jD74RgQ0-p7eg&s=10"
},
{
titulo: "Korn - Somebody someone",
archivo: "https://cs1.mp3.pm/listen/239693/aGdCUT
VSb2s5eGlDR20vVlpLYWhLclJrTTY4dURvZnB2L1E2ODVSWmlkblg5Vj
NteG9FTlhxWXVUS05Pb0tGNDEwU0NDdTVsbmxKMFpNUjJLeGVHZHpIND
NCdW0xSzlHbFpHeW52WjJubFdCcEJQbE91VnlmOVZFYmsxM2l4bFQ/Ko
rn_-_Somebody_Someone_(mp3.pm).mp3",
portada: "https://encrypted-tbn0.gstatic.com/ima
ges?q=tbn:ANd9GcR_qT3y6ztA47yIVSy-TiRRoYO14nLSrB97dW-rrTs0VA&s=10"
},
{
titulo: "Korn - Got the life",
archivo: "https://cs1.mp3.pm/listen/239697/aGdCU
TVSb2s5eGlDR20vVlpLYWhLclJrTTY4dURvZnB2L1E2ODVSWmlkbkNiNW
9IVE55Q1NjSGlCU1pRNXhwcjBtZXNIejU3U25KTXlGYVRXWlBOTU5IdHd
KY29QMWllY0pQM3JaM2hHbTlLaDhqR2p0bFdWL0d2MWZZQ2NzTHQ/Korn
_-_Got_The_Life_(mp3.pm).mp3",
portada: "https://encrypted-tbn0.gstatic.com/imag
es?q=tbn:ANd9GcSUV9hep0vqQvLoE566E-fDod8GaMX4iFyyDwmo3fiCUQ&s=10"
},
{
titulo: "Korn - Adidas",
archivo: "https://cs1.mp3.pm/listen/116030549/aGd
CUTVSb2s5eGlDR20vVlpLYWhLclJrTTY4dURvZnB2L1E2ODVSWmlkbVly
cVBHZmtXMmZSYzdmczEwWjFWbjlReFA1ck9FYkR3S2twdDNVa3lpSWl0W
EhTeGlzUVhSbW91Umxpelp1ZDZYK21UQUREcEpPeVZYdFFRS3hsaVA/Ko
rn_-_A.D.I.D.A.S.XD_(mp3.pm).mp3",
portada: "https://encrypted-tbn0.gstatic.com/imag
es?q=tbn:ANd9GcSHG7Qm9aKe0vIc6sLLy9ggpy1JfFE1VWR1SgBoFN6F5A&s=10"
},
{
titulo: "Korn - World Up",
archivo: "https://cs1.mp3.pm/listen/1525870/aGdCUT
VSb2s5eGlDR20vVlpLYWhLclJrTTY4dURvZnB2L1E2ODVSWmlkbGZFQUVO
UTJJeUVlRExhRVZEcWVvWExnKzM2QTZXUEVvckdhZWRtTkkyYmY1a2VUcX
BSVjg2Qktma28zb05TSllmVktCT0t2bVd5NWVpMWJlM1Q3V1E/Korn_-_W
ord_Up_Cameo_(mp3.pm).mp3",
portada: "https://encrypted-tbn0.gstatic.com/image
s?q=tbn:ANd9GcSlE7eKRXx-63fLgPbYUst-lPP1C_KBdQQ9Jb72xeu5lSSMGXxntyv7Dpgl&s=10"
},
{
titulo: "Korn - Here to stay",
archivo: "https://cs1.mp3.pm/listen/1893610/aGdCU
TVSb2s5eGlDR20vVlpLYWhLclJrTTY4dURvZnB2L1E2ODVSWmlkbnZTUj
BLeHNlb3lqTXFYWEJmd3V2S1ZkSHdIdFJtM0pWYyttNEVKaWNkTFNZZmN
MT0o4Tng0YTV5Q00yYXVZVFpiaTB1eXJ3c0pPNHEwQUxvWDBCcFM/Korn
_-_Here_To_Stay_Album_Version_(mp3.pm).mp3",
portada: "https://encrypted-tbn0.gstatic.com/imag
es?q=tbn:ANd9GcRFSQ7x5KCI9JSwtPIUPBW8vROcqfvcKrKczA_M-ddokg&s=10"
},
{
titulo: "Korn - Make me bad",
archivo: "https://cs1.mp3.pm/listen/1893648/aGdCUT
VSb2s5eGlDR20vVlpLYWhLclJrTTY4dURvZnB2L1E2ODVSWmlkbGZaMUNy
YkNSNHJ3d2VXMEpkRmdpMzcvZFRldlc4ZzJTaURHdTExQ3R1Zy95Z096cEF
jRTIycExkMzNmMDZVWW5vK3JTSmczcEJRd1gvV0xzTG9LRHI/Korn_-_Ma
ke_Me_Bad_Album_Version_(mp3.pm).mp3",
portada: "https://encrypted-tbn0.gstatic.com/images
?q=tbn:ANd9GcQvu4DMfCC1Ik6sHMiHYirR23Onl0AkO_tBcqCSOElhtg&s=10"
},
{
titulo: "Korn - Did my time",
archivo: "https://cs1.mp3.pm/listen/2736380/aGdCUT
VSb2s5eGlDR20vVlpLYWhLclJrTTY4dURvZnB2L1E2ODVSWmlkbWxsMlRR
amZFTW01WC9kTTVWK3VvNklpTDk4WkcyQzVHTUVHSElNYnVhYVl4OVVEd3
pacWRuUWpjcE92bUlNbHRTdmsraFRDb1hweEFhVzVtMFRUa0E/Korn_-_Did_My_Time_(mp3.pm).mp3",
portada: "https://encrypted-tbn0.gstatic.com/imag
es?q=tbn:ANd9GcTFsqgNns8XCYHMBZtw0S5UJeHeWsTYzJinNs03IXaCfQ&s"
}
];
const audio = document.getElementById("audio");
const titulo = document.getElementById("titulo");
const play = document.getElementById("play");
const lista = document.getElementById("lista");
const progreso = document.getElementById("progreso");
let indice = 0;
let aleatorio = false;
// Cargar canción sin reproducir
function cargarCancion(i) {
indice = i;
audio.src = canciones[indice].archivo;
titulo.textContent = canciones[indice].titulo;
// Cambiar automáticamente la carátula
document.getElementById("portada").src =
canciones[indice].portada;
audio.load();
actualizarLista();
play.textContent = "▶";
document.getElementById("estado").textContent =
"Listo para reproducir";
}
// Reproducir o pausar
play.addEventListener("click", () => {
if (audio.paused) {
audio.play();
play.textContent = "⏸";
} else {
audio.pause();
play.textContent = "▶";
}
});
// Siguiente canción
function siguienteCancion() {
if (aleatorio) {
if (canciones.length > 1) {
let nuevoIndice;
do {
nuevoIndice = Math.floor(
Math.random() * canciones.length
);
} while (nuevoIndice === indice);
indice = nuevoIndice;
}
} else {
indice = (indice + 1) % canciones.length;
}
cargarCancion(indice);
audio.play();
play.textContent = "⏸";
}
// Canción anterior
function anteriorCancion() {
indice = (indice - 1 + canciones.length) %
canciones.length;
cargarCancion(indice);
audio.play();
play.textContent = "⏸";
}
document.getElementById("siguiente")
.addEventListener("click", siguienteCancion);
document.getElementById("anterior")
.addEventListener("click", anteriorCancion);
// Activar o desactivar aleatorio
document.getElementById("aleatorio")
.addEventListener("click", function() {
aleatorio = !aleatorio;
this.classList.toggle("activo", aleatorio);
});
// Crear lista de canciones
function actualizarLista() {
lista.innerHTML = "";
// Mostrar solamente 5 canciones
const cantidadVisible = 5;
// Calcular inicio de la lista
let inicio = Math.max(
0,
indice - Math.floor(cantidadVisible / 2)
);
// Evitar espacios al llegar al final
inicio = Math.min(
inicio,
Math.max(0, canciones.length - cantidadVisible)
);
const fin = Math.min(
inicio + cantidadVisible,
canciones.length
);
// Crear únicamente las 5 canciones visibles
for (let i = inicio; i < fin; i++) {
const li = document.createElement("li");
li.textContent =
(i === indice ? "▶ " : "♫ ") +
canciones[i].titulo;
li.classList.toggle(
"activa",
i === indice
);
li.addEventListener("click", () => {
cargarCancion(i);
audio.play();
play.textContent = "⏸";
});
lista.appendChild(li);
}
}// Actualizar progreso
audio.addEventListener("timeupdate", () => {
if (audio.duration) {
progreso.value =
(audio.currentTime / audio.duration) * 100;
document.getElementById("actual").textContent =
formatoTiempo(audio.currentTime);
}
});
// Mostrar duración
audio.addEventListener("loadedmetadata", () => {
document.getElementById("duracion").textContent =
formatoTiempo(audio.duration);
});
// Mover progreso
progreso.addEventListener("input", () => {
if (audio.duration) {
audio.currentTime =
(progreso.value / 100) * audio.duration;
}
});
// Control de volumen
document.getElementById("volumen")
.addEventListener("input", function() {
audio.volume = this.value;
});
function formatoTiempo(segundos) {
if (!Number.isFinite(segundos)) return "0:00";
const minutos = Math.floor(segundos / 60);
const segundosRestantes =
Math.floor(segundos % 60);
return minutos + ":" +
String(segundosRestantes).padStart(2, "0");
}
// Reproducir siguiente al terminar
audio.addEventListener("ended", siguienteCancion);
// Actualizar estado
audio.addEventListener("play", () => {
document.getElementById("estado").textContent =
"Reproduciendo";
});
audio.addEventListener("pause", () => {
document.getElementById("estado").textContent =
"En pausa";
});
// Inicializar sin reproducción automática
audio.volume = 0.7;
cargarCancion(0);