2

I asked and found a solution with SystemVolumeDidChangeNotification never get triggered on iOS 14. The event would be triggered and can be observed in iOS 14 with the workaround I found.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    UIApplication.shared.beginReceivingRemoteControlEvents() // <- THIS LINE

    NotificationCenter.default.addObserver(self, selector: #selector(volumeChange(_:)), name: Notification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification"), object: nil)
}

However, after upgrading the system to iOS 15 the event stopped getting triggered.

XY L
  • 25,431
  • 14
  • 84
  • 143

1 Answers1

0

NOTE: This method does not work in the simulator. It only works with a real device.

Inside the ViewController class,

    internal var volumeValueObservation: NSKeyValueObservation?

Inside the ViewController life-cycle method,

        do {
            let audioSession = AVAudioSession.sharedInstance()

            try audioSession.setActive(true) // <- Important
            
            volumeValueObservation = audioSession.observe(\.outputVolume) { av, _ in
                print(">>> DEBUG: [ViewController:handleVolumeChange]", av.outputVolume)
            }
        } catch {}

REFERENCE

Swift 4 Using KVO to listen to volume changes

System Volume Change Observer not working on iOS 15

XY L
  • 25,431
  • 14
  • 84
  • 143