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

Hello! I got the same issue, even with this fix Audio: Fixed pause()/play() for looped audios. by Mugen87 Ā· Pull Request #19079 Ā· mrdoob/three.js Ā· GitHub
Case is that play/pause is working fine until the first repetition of looped piece. After audio is looped once, itā€™s always playing form the loopStart, but not from time when it was paused