Plugin.Maui.Audio icon indicating copy to clipboard operation
Plugin.Maui.Audio copied to clipboard

Fix iOS audio session conflicts between AudioStreamer and AudioPlayer

Open Copilot opened this issue 4 months ago • 0 comments

When AudioStreamer is initialized on iOS, subsequent AudioPlayer playback fails with OSStatus error 561017449. This occurs because AudioStreamer uses AVAudioSessionCategory.Record while AudioPlayer uses AVAudioSessionCategory.Playback - incompatible categories that cannot be active simultaneously.

Changes

  • AudioStreamOptions.macios.cs: Changed default category from Record to PlayAndRecord to support concurrent recording and playback
  • ActiveSessionHelper.macios.cs: Added retry logic that deactivates and reactivates the audio session when category changes fail, preventing conflicts when components initialize in different orders

The PlayAndRecord category is the standard iOS approach for apps that need simultaneous audio input and output capabilities.

Original prompt

This section details on the original issue you should resolve

<issue_title>Playing sounds does not work on IoS when AudioStreamer is initialized</issue_title> <issue_description>I've been working on my own voice recognition using plugin.maui,.audio and got that working on Android, IoS, and Windows.

I started to add in playback items like wake tones. This is when I noticed something odd. On Android, the sounds work and text to speech works great.

On IoS, there's nothing. However, if I pause the startup (setting a breakpoint), the audio does play at least the first time.

If I let my main program start up and run without breakpoints, I get an error

The operation couldn’t be completed. (OSStatus error 561017449.)

So I assume that has something to do with what is going on... It's something about resources are already allocated or being used (or something like that)

Loading a wav file like this:

playerIsAwake = AudioManager.Current.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("isAwake.wav"));

Both sound out lines: await TextToSpeech.Default.SpeakAsync(text, cancelToken: token).ConfigureAwait(false) and playerIsAwake.Play();

Work on Android and Windows after initializing and fail to play anything in IoS, unless I comment out the line: await _audioStreamer.StartAsync();

If that line is commented out, then the IoS playback works, but of course the streamer is not initialized.

Since my program has a lot of things in it, I decided to set up a minimal code test in an attempt to reproduce the issue.

When I did this, the above error goes away, probably due to the class injection in Maui which I'm not doing in this test app. However, the issue still exists. Android and Windows work great, IoS does not. (Again, unless I comment out the above line)

To help out, I created a repository with the demo code. You'll only need to add in your bundle signing to get it to deploy on IoS. The repository is here: [https://github.com/sej69/AudioManagerTest/tree/master/AudioManagerTest](GitHub AudioManagerTest)

</issue_description>

Comments on the Issue (you are @copilot in this section)

  • Fixes jfversluis/Plugin.Maui.Audio#196

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot avatar Nov 04 '25 14:11 Copilot