I am building a multitrack audio player in Swift using AVAudioPlayerNode. When scheduling an audio segment for playback, I want to be notified when the scheduling is complete AND when/if file playback finishes. The way the Swift API is written it appears that you can only register for one or the other. If you call the short function:
func scheduleSegment(_ file: AVAudioFile,
startingFrame startFrame: AVAudioFramePosition,
frameCount numberFrames: AVAudioFrameCount,
at when: AVAudioTime?,
completionHandler: AVAudioNodeCompletionHandler? = nil)
The callback gets called as soon as the file has been scheduled (not played, just scheduled to be played), and you cannot pass any special arguments, just a basic closure.
When calling the fancier function, you can specify which type of callback you would like, however you must pick one and only one:
func scheduleSegment(_ file: AVAudioFile,
startingFrame startFrame: AVAudioFramePosition,
frameCount numberFrames: AVAudioFrameCount,
at when: AVAudioTime?,
completionCallbackType callbackType: AVAudioPlayerNodeCompletionCallbackType,
completionHandler: AVAudioPlayerNodeCompletionHandler? = nil)
The enum AVAudioPlayerNodeCompletionCallbackType can be one of the following: dataConsumed, dataRendered, or dataPlayedBack
Unfortunately I must pick one, but really I need two. I need to be notified when scheduling is complete AND dataPlayedBack.
Is there a way to do this?