有没有办法在调度缓冲区时为每个循环都有一个完成处理程序?
self.audioPlayerNode.scheduleBuffer(buffer, at: nil, options: .loops, completionHandler: completionHandler)
// call each loop, not only on the buffer end
func completionHandler() {
// code here
}
有没有办法在调度缓冲区时为每个循环都有一个完成处理程序?
self.audioPlayerNode.scheduleBuffer(buffer, at: nil, options: .loops, completionHandler: completionHandler)
// call each loop, not only on the buffer end
func completionHandler() {
// code here
}
我能想到的唯一方法是重新调度同一个缓冲区,每次都提供一个完成处理程序。
我会这样做:
func scheduleNext() {
self.audioPlayerNode.scheduleBuffer(buffer, at: nil, completionHandler: completionHandler)
}
func completionHandler() {
// code here
scheduleNext()
}
// ...
scheduleNext()
这对我有用
// 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
}
})