Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

253
Vistas
How to play audio when a button is pressed?

I'm trying to make a website that plays audio when a button is pressed using HTML/CSS but i'm not sure how to make the button play the music file when pressed. Does anybody know how to do this? I can also use some Javascript but i'm not the best at it if it's required.

Also I am aware that this question exists, but it's not a duplicate because that's asking how to do it in python and tkinter, but i'm asking how to do it with HTML/CSS/JS.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

This will work best acc. to your use case.

const btn = document.getElementById("btn");

btn.onclick = () => {
  // Here you will add path to local file you have
  const audio = new Audio(
    "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3"
  );

  audio.play();
};
<!DOCTYPE html>
<html>
  <head>
    <title>Parcel Sandbox</title>
    <meta charset="UTF-8" />
  </head>

  <body>
    <div id="app">
      <button id="btn">Play Sound</button>
    </div>

    <script src="src/index.js"></script>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Yeah, JavaScript is pretty much required for this. You can use the audio element for this, something like so (codepen.io):

<button class="start-audio-btn">Click me!</button>
<!--  just some random mp3 I found -->
<audio src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3" class="sound"></audio>

And then add functionality in JavaScript:

// Get the button element
const button = document.querySelector('.start-audio-btn');
// Get the audio element
const sound = document.querySelector('.sound');
// Start the audio element's playback using the `play` method when the button's clicked
button.addEventListener('click', (_) => { sound.play(); });
about 4 years ago · Juan Pablo Isaza Denunciar

0

This would solve anything you would need dealing with audio => Play, Pause, Stop, Mute & Unmute :)

playBtn.onclick = () => audio.play()
stopBtn.onclick = () => {audio.pause(); audio.currentTime = 0;}
pauseBtn.onclick = () => audio.pause()
muteBtn.onclick = () => audio.muted = !audio.muted
<audio controls id='audio'>
  <source src="yourAudioSource" type="audio/yourAudioExtension">
  <!-- This text will be shown if a browser doesn't support the extension of your audio -->
  Your browser does not support the audio element.
</audio>

<button id="playBtn">Play</button>
<button id="stopBtn">Stop</button>
<button id="pauseBtn">Pause</button>
<button id="muteBtn">Toggle Mute</button>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda