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