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

289
Visualizações
How to stop an audio element created by a function? IE: audio = new Audio()

I'm creating a Canvas game and I'm using this to stop all audios:

  document.querySelectorAll("audio").forEach((el) => {
  if (
    el.id === "song1" ||
    el.id === "song2" ||
    el.id === "song3"
  ) {
    el.volume = 0;
    el.pause();
  } else {
    el.currentTime = 0;
    el.volume = 0;
    el.pause();
  }
});

The problem I have is that this stop all the audios within the <head></head> tag in the html file (preloaded ones), but it does not work for audios created dinamically

function soundGo(newAudio, boolean) {
  if (boolean === true) {
    let audio = new Audio(newAudio);
    audio.volume = 0.1;
    audio.play();
} else {
    let audio = new Audio(newAudio);
    audio.volume = 0;
    audio.pause();
}
}

I want to know if it's possible to iterate all the elements created with new Audio(). The querySelectorAll does not the trick, at least in my case (maybe I'm doing it wrong...).

I tried this also: stop dynamically created Audio

about 4 years ago · Juan Pablo Isaza
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