Search code examples
swiftvolumeios15

System Volume Change Observer not working on iOS 15


I used following code to detect system volume changed by users.

NotificationCenter.default.addObserver(self, selector: #selector(volumeChanged), name: NSNotification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification"), object: nil)

When I updated to iOS 15, I found that this code is not working, but for any previous versions of iOS it works.

I also used an addObserver function, but that is ok.

Is this a iOS 15 bug and if so what can I do to fix it.

thanks :)


Solution

  • What you're doing is unsupported, so it's not really surprising if it doesn't work on all systems. The correct documented approach is to use KVO on the audio session outputVolume property: https://developer.apple.com/documentation/avfaudio/avaudiosession/1616533-outputvolume