1

If I had an audio element, I could use something like audioElement.currentTime=5;

But how do I set the time for an audio buffer source in the Web Audio API? Is there anything like:

var source=context.createBufferSource(); source.currentTime=5;

To clarify, I want to play 5 seconds into the audio buffer's audio.

Josh Powlison
  • 723
  • 9
  • 15
  • Have you taken a look here? [HTML audio can't set currentTime](https://stackoverflow.com/questions/37044064/html-audio-cant-set-currenttime) – Mathiasfc Aug 14 '18 at 01:04
  • @MathiasFalci Thanks for the link, but they're discussing the audio element, not an audio buffer source. – Josh Powlison Aug 14 '18 at 02:38

2 Answers2

3

src.start() takes a second parameter for offset: https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start

src.start(when,offset,duration)

If I use src.start(0,5), I'll start the audio buffer 5 seconds in!

Josh Powlison
  • 723
  • 9
  • 15
-1

If you want an AudioBufferSourceNode to start playing from time 5, you have use src.start(5) or maybe src.start(5 + context.currentTime). This assumes that the AudioBuffer associated with the AudioBufferSourceNode has at least 5 sec of audio to play. Otherwise you'll hear nothing.

Raymond Toy
  • 5,490
  • 10
  • 13
  • 1
    src.start(x) determines how many seconds we'll wait until playing the sound though, not what time in the sound we'll play from. https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start However, there is a second parameter that you've made me aware of that does what I want. Thanks! :) – Josh Powlison Aug 14 '18 at 17:53