1

I have the following code in my app: DistributedNotificationCenter.default.addObserver(self, selector: #selector(volumeChangeEvent(_:)), name: NSNotification.Name(rawValue: "com.apple.sound.settingsChangedNotification"), object: nil)

As expected volumeChangeEvent(_:) is called when I change or mute the volume output. But only if the Preferences app is open...

Any suggestion is welcome.

Alex

AlexPera
  • 366
  • 5
  • 12
  • Having the same issue. DId you manage to get it to work? – pawisoon Jan 26 '20 at 12:10
  • No, still searching for a solution. I found something about Kvo values, but I have no idea about how to get them to work properly. Please tell me if you do. – AlexPera Jan 27 '20 at 13:47

1 Answers1

0

After trying countless ways I found a nice workaround: instead of searching for a probably non-existent notification that should tell when the volume had changed, I tried getting the physical key-press event. As the media keys are not sending a normal CGEvent I came up with this solution: Capture OSX media control buttons in Swift

Note that the TouchBar simulates such a key event, so any app that you'll write using this method will also work for those MacBook models which have the TouchBar.

It's probably not the ideal solution, but it works. If anyone knows a better way please let me know.

AlexPera
  • 366
  • 5
  • 12