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

118
Vistas
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 Respuestas
Responde la pregunta

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 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