暂停播放Three.js中的循环音频



我广泛使用ThreeJS的Audio和PositionalAudio部分,在我看来,播放/暂停功能无法正确用于循环音频。

在ThreeJS音频源中,我们可以阅读:

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

如果我理解正确的话,_pausedAt因此在某种程度上是音频文件中播放的持续时间,它很好,完全可用(特别是对于没有循环的直接播放(。但当执行play()功能时,我看到:

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

并且在AudioBufferSourceNode.start()的MDN文档中:

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.

这意味着,一旦文件被读取至少一次直到结束(有或没有暂停(,下一个play()(通常在pause()之后(将从文件的末尾开始(如前所述的duration和/或loopEnd(,而不是从暂停的正确位置重新启动——在loopStartloopEnd之间的某个位置。

我的理解正确吗?

如果是这样的话,我看不出比做下面这样的代码来纠正播放头(或_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;
}

这些有道理吗?

非常感谢

Benjamin

关于ThreeJS话语的回答:https://discourse.threejs.org/t/pause-for-looped-audio/16894此处已修复:https://github.com/mrdoob/three.js/pull/19079

您联系Benjamin的其他方式是什么?我不能在ThreeJS论坛上发帖。

最新更新