Search code examples
web-audio-api

webaudio, audiocontext jump to position


In my code I set AudioContext.currentTime to jump to a different position in the playing track. This does not work. It's old code, so I am not sure if the webaudio specifications have changed or I just created wrong code. I've been searching for a solution to how this should work for ages now. The only solutions I have seen, seem to involve stopping the old sound and creating a new buffersource.

is there no way to specify the position within the playing track?

edit: I am aware that AudioContext.currentTime is now specified as readonly


Solution

  • I do it like this:

      function playAudio(buffer, offsetInSeconds, duration) {
        console.log('start playing audio');
        var audioTrack = context.createBufferSource();
        audioTrack.connect(context.destination);
        audioTrack.buffer = buffer;     
        audioTrack.start(context.currentTime, offsetInSeconds,  duration);        
      }
    

    And yes, you have to create new bufferSource everytime.