Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!