3

Is there a way to have a completion handler for each loop when scheduling a buffer?

self.audioPlayerNode.scheduleBuffer(buffer, at: nil, options: .loops, completionHandler: completionHandler)

// call each loop, not only on the buffer end
func completionHandler() {
    // code here
}

2 Answers2

1

The only way I can think of is re-scheduling the same buffer, providing a completion handler each time.

I would go for this :

func scheduleNext() {
    self.audioPlayerNode.scheduleBuffer(buffer, at: nil, completionHandler: completionHandler)
}

func completionHandler() {
    // code here

    scheduleNext()
}

// ...
scheduleNext()
Vince
  • 525
  • 1
  • 3
  • 19
0

Here's what work for me

// audioFile here is our original audio

audioPlayerNode.scheduleFile(audioFile, at: nil, completionHandler: {
        print("scheduleFile Complete")

    var delayInSeconds: Double = 0

    if let lastRenderTime = self.audioPlayerNode.lastRenderTime, let playerTime = self.audioPlayerNode.playerTime(forNodeTime: lastRenderTime) {

        if let rate = rate {
            delayInSeconds = Double(audioFile.length - playerTime.sampleTime) / Double(audioFile.processingFormat.sampleRate) / Double(rate!)
        } else {
            delayInSeconds = Double(audioFile.length - playerTime.sampleTime) / Double(audioFile.processingFormat.sampleRate)
        }
    }

    // schedule a stop timer for when audio finishes playing
    DispatchTime.executeAfter(seconds: delayInSeconds) {
        audioEngine.mainMixerNode.removeTap(onBus: 0)
        // Playback has completed
    }

})
ZaEeM ZaFaR
  • 1,508
  • 17
  • 22