Pause for looped audio

Hi all,

I extensively use the Audio and PositionalAudio part of ThreeJS and it seems to me that the play/pause function can not work correctly for a looped audio.
Indeed in the ThreeJS Audio source we can read:

this._pausedAt += Math.max( this.context.currentTime - this._startedAt, 0 ) * this.playbackRate;

If in understand correctly _pausedAt is thus somehow the elapsed duration of playint the audio file which is fine and totally usable, in particular for a straightforward playing with no loop. But when executing the play() function I see:

source.start( this._startedAt, this._pausedAt + this.offset, this.duration );

And in the MDN documentation of AudioBufferSourceNode.start(): offsets past the end of the audio which will be played (based on the audio buffer’s duration and/or the loopEnd property) are silently clamped to the maximum value allowed.

That means that once the file has been read at least once until the end (with or without pauses), the next play() (typically after a pause()) will start at the end of the file (duration and/or loopEnd as mentionned) instead of restarting from the correct position where it was paused – somewhere in between loopStart and loopEnd.

Am I correct on this understanding?

And if so, I see no better option than doing something like the code below to correct the position of the playhead (or _pausedAt):

  if (sound.getLoop() === true) {
    var looping = (sound.loopEnd === 0) ? sound.source.buffer.duration : sound.loopEnd
    var loopDur = looping - sound.loopStart
    _pausedAt = (_pausedAt - sound.loopStart) % loopDur + sound.loopStart;
  }

Where sound is a THREE.Audio() instance.
Does any of this make sense to you?

Thank you very much,

Benjamin

/cc

1 Like

Yes,
That’s my post also! I double posted because I have no idea where would be more reactive… sorry if that’s an inconvenience.

This should be fixed since r116, see:

If not, can you please demonstrate the issue with a live example?

Ow! Excellent, thank’s
It seems that I’m not up to date. I’m going to co this release at once.
And let you know…

I probably should have searched in the github as well and not only on the discourse and stackoverflow. Sorry.

2 Likes