(ios) crash fix: conditionally set AVAudioSession category on load#126
Open
dpa99c wants to merge 1 commit intocapacitor-community:masterfrom
Open
(ios) crash fix: conditionally set AVAudioSession category on load#126dpa99c wants to merge 1 commit intocapacitor-community:masterfrom
dpa99c wants to merge 1 commit intocapacitor-community:masterfrom
Conversation
… it to "playback" if it's "record", as any other category will allow playback.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Analysis
In the current plugin implementation for iOS, if another plugin or library uses the shared
AVAudioSessioninstance, it can conflict with this plugin, leading to an app crash with an error such as this:In my case, this happened when updating to Capacitor v7, which changes how plugins are initialized at startup, and they now load earlier.
This caused a race condition between the
load()function of this plugin and the initialization of another native audio library I'm using.The other library is for audio conversations, so requires the playAndRecord AVAudioSession category to be set.
However, currently this plugin immediately sets the playback category on loading.
In the race condition, the other library sets an initial category of "playbackAndRecord", then this plugin's
load()function runs and sets the category to "playback", so when the other library tries to initialize an audio session with record/playback functionality, the category is wrong, causing the app to crash.Solution
My solution is to change the
load()function of this plugin to check the current session category and only set it to "playback" if it's currently set to "record", which is the only category where playback will not work.Any other category (e.g.
playbackAndRecord) will allow playback.This prevents any issues if another native class has set a different audio session category prior to this plugin initializing.