Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created September 28, 2020 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/178799804feb5f8d6b8d2033481faf1f to your computer and use it in GitHub Desktop.
Save parzibyte/178799804feb5f8d6b8d2033481faf1f to your computer and use it in GitHub Desktop.
// Carga un sonido a través de su fuente y lo inyecta de manera oculta
const cargarSonido = function (fuente) {
const sonido = document.createElement("audio");
sonido.src = fuente;
sonido.setAttribute("preload", "auto");
sonido.setAttribute("controls", "none");
sonido.style.display = "none"; // <-- oculto
document.body.appendChild(sonido);
return sonido;
};
const $botonReproducir = document.querySelector("#btnReproducir"),
$botonPausar = document.querySelector("#btnPausar"),
$botonReiniciar = document.querySelector("#btnReiniciar");
// El sonido que podemos reproducir o pausar
const sonido = cargarSonido("sonido.flac");
$botonReproducir.onclick = () => {
sonido.play();
};
$botonPausar.onclick = () => {
sonido.pause();
};
$botonReiniciar.onclick = () => {
sonido.currentTime = 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment