Reproductor de música para tu sitio web
HTML5
<div class="player">
<h1>►► ♫ Música ♪ ◄◄</h1>
<div class="cover">
<img id="portada"
src="caratulas/portada1.jpg"
alt="Carátula del álbum">
</div>
<h2 id="titulo">Selecciona una canción</h2>
<p id="estado">Listo para reproducir</p>
<audio id="audio"></audio>
<input type="range" id="progreso"
value="0" min="0" max="100">
<div class="tiempos">
<span id="actual">0:00</span>
<span id="duracion">0:00</span>
</div>
<div class="controles">
<button id="aleatorio" title="Aleatorio"><img src="
https://images.icon-icons.com/8936/PNG/96/467909_shuffle-icon.png"
alt="" style="height: 20px; vertical-align: middle;"></button>
<button id="anterior">⏮</button>
<button id="play">▶</button>
<button id="siguiente">⏭</button>
</div>
<div class="volumen">
🔊
<input type="range" id="volumen"
min="0" max="1" step="0.01" value="0.7">
</div>
<h3>Lista de canciones</h3>
<ul id="lista"></ul>
</div>
<script src="javascript.js"></script>
CSS
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
width: 100%;
font-family: Arial, sans-serif;
background: linear-gradient(135deg, #141e30, #243b55);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.player {
background: url(img/ipodfondo.jpg) no-repeat center /cover;
width: 100%;
max-width: 380px;
padding: 25px;
border-radius: 20px;
text-align: center;
box-shadow: 0 10px 35px #0005;
border: 1px solid rgb(83, 83, 83);
}
h1 {
font-size: 18px;
font-weight: bolder;
margin-bottom: 10px;
color: rgb(145, 255, 0);
-webkit-text-stroke-width: .4px;
-webkit-text-stroke-color: rgb(18, 81, 199);
}
#lista {
background: rgb(233, 233, 233);
list-style: none;
text-align: left;
height: 260px;
border-radius: 6px;
overflow-y: auto;
overflow-x: hidden;
box-sizing: border-box;
scrollbar-color: #727272 #c7c7c7;
}
#lista li {
height: 52px;
display: flex;
align-items: center;
padding: 12px;
border-bottom: 1px solid #ddd;
cursor: pointer;
}
.cover {
width: 100%;
height: 250px;
background: #243b55;
border-radius: 15px;
overflow: hidden;
margin: 5px 0px;
box-shadow: 0px 10px 17px 0px rgba(0,0,0,0.75);
-webkit-box-shadow: 0px 10px 17px 0px rgba(0,0,0,0.75);
-moz-box-shadow: 0px 10px 17px 0px rgba(0,0,0,0.75);
}
.cover img {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
h2 {
font-family: Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif;
font-size: 19px;
font-weight: bolder;
color: rgb(145, 255, 0);
-webkit-text-stroke-width: .1px;
-webkit-text-stroke-color: black;
}
#estado {
color: #afdb11;
font-size: 13px;
margin-bottom: 20px;
}
#progreso {
width: 100%;
cursor: pointer;
}
.tiempos {
display: flex;
justify-content: space-between;
font-size: 12px;
margin-top: 5px;
}
span{
color: cornsilk;
}
.controles {
display: flex;
justify-content: center;
align-items: center;
gap: 15px;
margin: 20px 0;
}
button {
border: none;
background: cornsilk;
color: rgb(97, 92, 70);
padding: 12px;
border-radius: 50%;
cursor: pointer;
font-size: 18px;
transition: .2s all linear;
}
button:hover {
background: rgb(209, 203, 179);
color: rgb(243, 231, 183);
}
#play {
background: rgb(233, 226, 199);
color: rgb(97, 92, 70);
width: 55px;
height: 55px;
font-size: 23px;
}
#aleatorio {
background: cornsilk;
}
#aleatorio.activo {
background: cornflowerblue;
}
.volumen {
display: flex;
justify-content: center;
align-items: center;
gap: 10px;
margin-bottom: 20px;
}
#volumen {
width: 120px;
}
h3 {
color: cornsilk;
text-align: left;
margin-bottom: 10px;
}
#lista {
list-style: none;
text-align: left;
max-height: 180px;
overflow-y: auto;
}
#lista li {
padding: 12px;
border-bottom: 1px solid #ddd;
cursor: pointer;
background: cornsilk;
}
#lista li:hover {
background: rgb(223, 216, 189);
}
#lista li.activa {
color: #1c7bbb;
font-weight: bold;
}
JS
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);
