fix(ios): Configure audio session before activation and only if playing #4735
+1
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
There's an issue on iOS that even if the player is configured to mix audio with others and even if the player is muted, when the player is mounted it steals audio focus from possible background audio.
Motivation
Fixes #4610
We've been running this change as a patch in our production app for a couple of months with no issues and it fixed the linked issue.
Changes
Configuring the audio session only if there's an existing player and configuring the audio session before activating it.
The idea in the fix is that we do not even try to configure the audio session if there there are no players playing, since it's not possible to configure it correctly unless there are some existing players where we would get the configuration values from, so it ends up defaulting in possibly incorrect audio category/mode in that case. Also do the configuration before activating the audio session since that's what Apple recommends:
"Typically, you set the category and mode before activating the session. You can also set the category or mode while the session is active, but doing so results in an immediate change." from: https://developer.apple.com/documentation/avfaudio/avaudiosession/setcategory(_:mode:options:)
Test plan