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, and 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.
Here is how I configure the audio session:
await audioSessionIOS.configure(AudioSessionConfiguration(
avAudioSessionCategory: AVAudioSessionCategory.playback,
avAudioSessionMode: AVAudioSessionMode.defaultMode,
avAudioSessionCategoryOptions: AVAudioSessionCategoryOptions.mixWithOthers));
Does anyone have any idea on how I can fix this?
I tried setting the AudioSession category to AVAudioSessionCategory.ambient but it doesn't work even on iPhone 6s.
Edit 1
I also tried adding listeners to begin/end interruptions, becomingNoisyEventStream, and devicesChangedEventStream. But the 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 uses 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, 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 has iOS 15.6 which is lower than the 6's version.
Edit 2
I moved the audio session configuration and activation to the main() method of my app. This allowed background replay of audio on iPhone 13 iOS 15.6. However, it still doesn't work on iPhone 13 iOS 16.6.
This means the problem might be related to iOS changes between versions 15.6 and 16.6.
I didn't find any related changes in the release notes here https://developer.apple.com/documentation/ios-ipados-release-notes/ios-ipados-15_6-release-notes. I'll keep digging.