Handling AVAudioEngine Configuration Change

Hi all,

I have been quite stumped on this behavior for a little bit now, so thought it best to share here and see if someone more experience with AVAudioEngine / AVAudioSession can weigh in.

Right now I have a AVAudioEngine that I am using to perform some voice chat with and give buffers to play. This works perfectly until route changes start to occur, which causes the AVAudioEngine to reset itself, which then causes all players attached to this engine to be stopped.

Once a AVPlayerNode gets stopped due to this (but also any other time), all samples that were scheduled to be played then get purged. Where this becomes confusing for me is the completion handler gets called every time regardless of the sound actually being played.

Is there a reliable way to know if a sample needs to be rescheduled after a player has been reset?

I am not quite sure in my case what my observer of AVAudioEngineConfigurationChange needs to be doing, as this engine only handles output. All input is through a separate engine for simplicity.

Currently I am storing a queue of samples as they get sent to the AVPlayerNode for playback, and after that completion checking if the player isPlaying or not. If it's playing I assume that the sound actually was played- and if not then I leave it in the queue and assume that an observer on the route change or the configuration change will realize there are samples in the queue and reset them

Thanks for any feedback!

Hello @draff27, thank you for your post.

Where this becomes confusing for me is the completion handler gets called every time regardless of the sound actually being played.

When you schedule a file or buffer, you can also specify a callback type. Please see AVAudioPlayerNodeCompletionCallbackType. This callback type allows you to differentiate between the player having consumed the data, rendered, or finished playing it back.

Handling AVAudioEngine Configuration Change
 
 
Q