Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

251
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda