AVAudioPlayerNode 播放指定次数的声音

AVAudoPlayerNode to play a sound for a defined number of times

是否可以在AVAudioPlayerNode 中只循环多次而不是无限次地循环播放音频声音?下面的代码将音频安排了无限次而不是说 5 次。

aVAudioPlayerNode.scheduleBuffer(buffer!, at: nil, options: AVAudioPlayerNodeBufferOptions.loops, completionHandler: nil)

我看起来与 AVAudioPlayer 类似,因为 numberOfLoops 定义播放音频的次数。我可以使用 AVAudioPlayer,但没有播放音高的功能。

scheduleBuffer 文档说 "Schedules the buffer to be played following any previously scheduled commands."。所以就安排多次吧。

extension AVAudioPlayerNode {
    func sceduleBuffer(_ buffer: AVAudioPCMBuffer, numberOfLoops: Int = 1) {
        for _ in 0..<numberOfLoops { scheduleBuffer(buffer, at: nil) }
    }
}