Tips for Managing AVAudioSession API and Issues with AirPods

Mobile device
SHARE

During audio development on our iOS application, the team at Red5 Pro found that Apple’s AirPods cause rendering issues while subscribing to a stream after they have been successfully paired with a device. As it turns out, there are a few limitations to Apple’s Bluetooth headsets which alter the stream in unexpected ways. The good… Continue reading Tips for Managing AVAudioSession API and Issues with AirPods

During audio development on our iOS application, the team at Red5 Pro found that Apple’s AirPods cause rendering issues while subscribing to a stream after they have been successfully paired with a device. As it turns out, there are a few limitations to Apple’s Bluetooth headsets which alter the stream in unexpected ways. The good news is that if you are facing AV sync or other microphone issues when using AirPods with your iOS application, we may have a solution to your problem.

Red5 Pro has several mobile testbed examples that demonstrate how to maintain the audio from a stream while the mobile app is in the background. In the iOS examples, the app listens for events in the Notification Center to recognize when the app goes to the background. Specifically, it listens for the UIApplication.willResignActiveNotification and pauses video rendering upon finding it. This worked well for a variety of scenarios, including alerts of incoming phone calls or the user hitting the home button while the stream is active. Recently, however, we saw an issue related to the notification presented while pairing AirPods to the device.

When AirPods pair with a device, they present an alert that causes the currently-open application to move to the background briefly. In our iOS testbed, the background tests behaved as expected; they paused rendering and maintained audio streaming while the AirPods pairing notification was present on the screen. However, once the notification is gone, the app listens for a specific UIApplication.willEnterForegroundNotification event, and with AirPods this event was never invoked. Instead, the app attempts to move directly to a different event,  UIApplication.didBecomeActiveNotification, without first switching the app back to the foreground. We noticed that the application stopped using the AVAudioSession defined for our SDK because the callback assigned to process audio frames stopped being called once the AirPods had successfully connected and resumed being called if they were disconnected.

Finding a Solution for the AirPod Pairing Issue While Streaming

After some research, it became clear that the main issue with the AirPods we tested is that they do not support audio sessions with the PlayAndRecord category, which is what our subscriber audio session used. Due to this limitation, when an audio session becomes active, the device takes over the current audio session and replaces it with one that the AirPods support. This audio takeover defaults to a frame size of 1156, which is too small for our application, therefore causing an exception and pausing playback.

We found that overriding the audio takeover and setting the category of the subscriber audio session to Playback instead of PlayAndRecord fixes the problem, so that the session works as expected with a sample rate of 44100. This allows audio to switch to the headphones when they are connected and back to speakers when disconnected, while continuing to render the stream. Note that this is specific to Apple’s Bluetooth headsets and was not seen with other Bluetooth headsets that we tested.


If you’re running into AV sync issues during iOS development, this might be the solution for you. Let us know if we were able to help by joining our Slack community or contacting us at info@red5.net.