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