How can I detect when the mute switch has been toggled?

Hi, I’m working on an app that contains a feed of videos. The app has a global mute state flag that can be enabled or disabled. When the app mute state is toggled, I toggle the isMuted flag on my AVPlayers, and I also toggle the AVAudioSession category between .ambient and .playback.

I enable/disable the mute state when the user taps a software button in my app. I also enable/disable the mute state when the user toggles the iPhone physical mute switch. This behavior is extremely similar to how mute state works in the Instagram app.

However, my physical mute switch detection mechanism is quite hacky/finicky. I’m using a framework called Mute that I found on Github: https://github.com/akramhussein/Mute. It generally works, but it is far from a perfect solution, in part because it mis-triggers if the app hangs for a second (especially on older devices).

How can I more reliably detect the state of the iPhone’s physical mute switch? It certainly must be possible, because mute switch detection in the Instagram app is much faster and more reliable than it is in my app.

In case it’s useful, I submitted Feedback about this back in January (https://feedbackassistant.apple.com/feedback/7545314), but have not yet heard back.

Thank you!
I'm not sure of the right solution (I worked on this stuff way too many years ago), but you are working against the physical mute switch, which will mute your audio automatically if your category is .ambient. Back in the day (maybe still now) we specifically didn't let apps see the mute switch state, to avoid them trying to do for themselves what should be common behavior throughout the system (driven by the app's declared AVAudioSession category).
Thanks for getting back to me. I totally understand that it's cleaner to let the system be consistent about mute behavior based on the AudioSession category, but we really need the ability to toggle between the ambient category (to let the user browse passively while, say, listening to music from another app) and the playback category (so the user can hear the audio from our app).

The Instagram app updates its software mute button UI when the physical mute switch is toggled, so it must be possible? Any suggestions on how we can achieve the same thing?
How can I detect when the mute switch has been toggled?
 
 
Q