Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :)

like image 592
Gyeom Avatar asked Dec 06 '22 08:12

Gyeom


1 Answers

I hooked MPVolumeControllerSystemDataSource's method _systemVolumeDidChange and at iOS 15.0(at least beta2) the notification name has changed to SystemVolumeDidChange and here is the new notification structure:

{
    AudioCategory = "Audio/Video";
    Reason = ExplicitVolumeChange;
    SequenceNumber = 1069;
    Volume = 0;
}

There're two points to be noticed:

  1. This notification at iOS 15(at least in beta2) will be called twice even if you press volume button once, but their SequenceNumber is equal;
  2. This notification callback is not on main thread.
like image 67
AdamWang Avatar answered Jan 01 '23 06:01

AdamWang