Background audio not playing on iPhone 13+

Hello everyone,

I'm using Flutter and the just_audio package.

When a user receives a push notification, the app plays audio in the background.

I've tested this functionality on iPhone 6s and iPhone 13.

It works correctly on iPhone 6s and the app plays the sound on push notification received.

However on iPhone 13 the app receives the notification, starts the background process but fails to play the sound with these errors:

mediaserverd(MediaExperience)[17680] <Notice>: -CMSUtilities- CMSUtility_IsAllowedToStartPlaying: Client sid:0x45107e5, Runner(28933), 'prim' with category MediaPlayback and mode Default and mixable does not have assertions to start mixable playback
mediaserverd(MediaExperience)[17680] <Notice>: -CMSessionMgr- MXCoreSessionBeginInterruption_WithSecTaskAndFlags: CMSessionBeginInterruption failed as client 'sid:0x45107e5, Runner(28933), 'prim'' has insufficient privileges to take control
mediaserverd(AudioSessionServer)[17680] <Error>:  AudioSessionServerImp.mm:405   { "action":"cm_session_begin_interruption", "error":"translating CM session error", "session":{"ID":"0x45107e5","name":"Runner(28933)"}, "details":{"calling_line":879,"error_code":-16980,"error_string":"Operation denied. Cannot start playing"} }

From what I understand of these errors is that on the newer iPhones, there must be additional permissions.

Does anyone have any idea on how I can fix this?

Accepted Reply

This is how I configure the AudioSession:

await audioSessionIOS.configure(AudioSessionConfiguration(
    avAudioSessionCategory: AVAudioSessionCategory.playback,
    avAudioSessionMode: AVAudioSessionMode.defaultMode,
    avAudioSessionCategoryOptions: AVAudioSessionCategoryOptions.mixWithOthers));

I also tried adding listeners to begin/end interruptions, becomingNoisyEventStream, and devicesChangedEventStream. But error is still the same:

'prim' with category MediaPlayback and mode Default and mixable does not have assertions to start mixable playback
'prim'' has insufficient privileges to take control
translating CM session error
-16980 Operation denied. Cannot start playing
PlatformException(561015905, Session activation failed, null, null)

The error leads to https://developer.apple.com/documentation/avfaudio/avaudiosession/errorcode/cannotstartplaying. Which states:

An error code that indicates an attempt to start audio playback when it wasn’t allowed.
This error type can occur if the app’s Information property list doesn’t permit audio use. It can also occur if the app is in the background and using a category that doesn’t allow background audio.

I'm sure I have the audio in UIBackgroundModes of Info.plist. And I'm sure it's in the right place. I'm saying this because I have ImageNotification extension, which receives the push notifications. But it doesn't execute the handle. It calls a separate isolate that is related to the main app process. I know this because I traced the packageName and appName to the main Info.plist and not the extension's Info.plist.

The category is playback which should allow for background usage. Especially with mixWithOthers. But for some reason it has insufficient privileges to take control.

Again, this doesn't happen on iPhone 6s. It works correctly with that real device. The error is on iPhone 13. We tested with different iOS versions. iPhone 6s has iOS 15.7.9. We tested on two iPhone 13 devices. One of them has iOS 15.6 which is lower than the 6's version.

Replies

This is how I configure the AudioSession:

await audioSessionIOS.configure(AudioSessionConfiguration(
    avAudioSessionCategory: AVAudioSessionCategory.playback,
    avAudioSessionMode: AVAudioSessionMode.defaultMode,
    avAudioSessionCategoryOptions: AVAudioSessionCategoryOptions.mixWithOthers));

I also tried adding listeners to begin/end interruptions, becomingNoisyEventStream, and devicesChangedEventStream. But error is still the same:

'prim' with category MediaPlayback and mode Default and mixable does not have assertions to start mixable playback
'prim'' has insufficient privileges to take control
translating CM session error
-16980 Operation denied. Cannot start playing
PlatformException(561015905, Session activation failed, null, null)

The error leads to https://developer.apple.com/documentation/avfaudio/avaudiosession/errorcode/cannotstartplaying. Which states:

An error code that indicates an attempt to start audio playback when it wasn’t allowed.
This error type can occur if the app’s Information property list doesn’t permit audio use. It can also occur if the app is in the background and using a category that doesn’t allow background audio.

I'm sure I have the audio in UIBackgroundModes of Info.plist. And I'm sure it's in the right place. I'm saying this because I have ImageNotification extension, which receives the push notifications. But it doesn't execute the handle. It calls a separate isolate that is related to the main app process. I know this because I traced the packageName and appName to the main Info.plist and not the extension's Info.plist.

The category is playback which should allow for background usage. Especially with mixWithOthers. But for some reason it has insufficient privileges to take control.

Again, this doesn't happen on iPhone 6s. It works correctly with that real device. The error is on iPhone 13. We tested with different iOS versions. iPhone 6s has iOS 15.7.9. We tested on two iPhone 13 devices. One of them has iOS 15.6 which is lower than the 6's version.