1

For Web Audio API, is there a way to change the duration for start(); e.g. start(0,0,3), after the sound has been played for 2 seconds, now I want it to play for 5 seconds instead of 3.

or

do I have to schedule a successive start() to pick up where the previous left off?

Thanks in advance.

1 Answers1

0

Short answer: No. You can't change the duration set using the argument.

Long answer:

So start accepts 3 arguments, when, offset, duration. But all of those are optional. If you don't pass in a duration argument, it will keep playing till the end of the buffer (or keep looping if you have loop property set to true).

So one way to do what you want is to not pass in a duration argument to start and then call a stop function when you want it to stop playing (after 5s). stop can also be scheduled in advanced using a when parameter. So in your case it would be calling something like stop(context.currentTime + 3) at the 2s mark.

notthetup
  • 1,129
  • 9
  • 17
  • Thanks, that solved my question, but now a new question stems from the stop() solution. duration would not actually terminate the sound buffer but stop() will, if I do want to play the same range in the sound buffer indefinitely (say based on changing user input), what is a good approach in doing so. – Yuliang Wang Jan 06 '15 at 04:26
  • You could call the `stop` function in the callback of the user input event. That way you can stop immediately (or after a delay) from when the user input happens. – notthetup Jan 07 '15 at 02:06
  • Sorry, I think I might not have convey my question clearly, by indefinitely, I meant as in loops. Say, I want to play a sound from point A to point B (with point B being dynamically changeable) a number of times, if stop is used after the definition of point B, then I could not replay from point A to point B again. – Yuliang Wang Jan 07 '15 at 08:56
  • OK. So you're looking to play the audio (in a loop), till some user input event. Then figure out how many times the loop was played and play it back that many number of times again. You can do this using the `context.currentTime` high resolution timestamp that each `AudioContext` maintains. You can record the time when `start` is invoked and record when `stop` is invoked (through a user input event). The difference is how long the buffer was played (in a loop). How can replay a new buffer using `start` and stop at the same duration using `stop(value_of_difference)`. – notthetup Jan 07 '15 at 10:36
  • Or if you need to know how many times it was looped, you can just divide the `value_of_difference` by the length of the buffer. This of course won't work if your `playbackRate` is being changed while the buffer is playing. – notthetup Jan 07 '15 at 10:38
  • 1
    Thanks! Actually I just figured out what went wrong with my previous experiments. The reason why the stop() "terminated" the play was because I set its argument to an absolute figure like 3, which upon second loop of play, the AudioContext's currentTime has passed 3 second thus the sound will not be again played. – Yuliang Wang Jan 07 '15 at 11:39
  • Yes. All timestamps in WebAudio are with in the same scheme as the `currentTime` property. – notthetup Jan 07 '15 at 13:37