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

115
Visualizações
How can I play an audio file at specified times in JavaScript AudioContext?

How can I play an audio file at specified times in JavaScript AudioContext ?

    const context = new AudioContext();
    const source = context.createBufferSource();
    const audioBuffer = await fetch('./Sound.wav')
      .then(res => res.arrayBuffer())
      .then(ArrayBuffer => context.decodeAudioData(ArrayBuffer));

    source.buffer = audioBuffer;
    source.connect(context.destination);
    source.start(1,3,5,7,20,30,40); // I want to play many times this file at these seconds. (Sound.wav length is 1second)
 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The AudioBufferSourceNode#start() method accepts optionals offset and duration parameters in seconds as its second and third params (the first one being the usual when to start playing).

However you can start such a node only once (even though you can make it loop). So you will have to create a new node every time you want to start playing your audio file. But don't worry, these nodes have an extremely small footprint and won't eat your memory.

(async () => {
  const url = "https://upload.wikimedia.org/wikipedia/en/transcoded/d/dc/Strawberry_Fields_Forever_%28Beatles_song_-_sample%29.ogg/Strawberry_Fields_Forever_%28Beatles_song_-_sample%29.ogg.mp3";
  const context = new AudioContext();
  const audioBuffer = await fetch(url)
    .then(res => res.arrayBuffer())
    .then(buffer => context.decodeAudioData(buffer));

  const play = (startTime, duration) => {
    const source = context.createBufferSource();
    source.buffer = audioBuffer;
    source.connect(context.destination);
    source.start(context.currentTime, startTime, duration);
  };

  [1, 3, 5, 10, 15, 18].forEach((startTime) => {
    const btn = document.createElement("button");
    btn.onclick = (evt) => play(startTime, 1);
    btn.textContent = "start at " + startTime + "s";
    document.body.append(btn);
  });
})().catch(console.error)
body {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 5px;
}

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