Android loses audio when connecting or disconnecting bluetooth headphones

I’m having an issue where all playing audio stops playing when I connect or disconnect a bluetooth headset. I can find very little information on this issue online, which seems like a major issue that would effect everyone if this is an engine bug.

Am I doing something wrong, or is this an actual bug with Unity? I’m on Unity 2022.3.31f1

If you change audio device the Unity audio system has to restart - which involves stopping anything playing.

Does mention this

It is important to note that whenever runtime modifications of the global audio system configuration are performed all audio objects have to be reloaded.

but its not that clear that this applies to system changes - until you match it up with

changes may come from outside in response to a device change

hope this helps

So it is a bug with Unity then. That’s crazy that no one is complaining about this issue that I can tell. You’d think if the engine has to restart the audio system, that they would make sure to restore the state of audio that was playing…

I wouldn’t call it a bug, its a known limitation.

Currently the best way to deal with this is to subscribe to Unity - Scripting API: AudioSettings.OnAudioConfigurationChanged and handle the audio source restarting logic there.

We agree its really not a nice way to deal with things and are 100% looking into better systems in the future, but can not promise any timelines.

That’s not really a solution on its own. You then have to track every single audio that is playing, where their current position, and then restart them. And by the time that event has been called, the audio that was playing stopped and its position was already lost. I even managed a work around for it by doing something else, but then the volume was different when you switched between speakers and bluetooth.

If you want to call it a bug or a limitation in Unity doesn’t really affect the end result, in that this is a very common scenario that a game engine should be able to handle. Users shouldn’t expect to lose their audio if they decide they need to switch to headphones to not disturb someone next to them, and Unity shouldn’t leave it to game devs to solve an issue that effects every single developer putting a game on Android.