Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen to volume button presses the correct way (iOS 15)

I have used

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

in the past to listen to volume control changes however this no longer works in iOS 15.

This alternative code below only works well if you don't care about a user pressing the volume up when at max volume. However, I do want to know every time the button is pressed.

AVAudioSession.sharedInstance().addObserver(self, forKeyPath: "outputVolume", options: NSKeyValueObservingOptions.new, context: nil)

What is the correct way to listen to the user pressing the volume control button even when the volume is at its max/min?

like image 651
aaaav Avatar asked Jul 18 '21 21:07

aaaav


People also ask

How do you fix volume buttons on iOS 15?

Make Sure Your iPhone's Volume Buttons Are Enabled To check this, head to Settings > Sounds & Haptics and enable Change with Buttons. If this isn't enabled, this is why your volume buttons aren't working. If it's already enabled, proceed to the next fix.

Why can't I adjust my iPhone volume?

If the buttons don't change anything, go to Settings > Sounds & Haptics, then check that Change with Buttons is turned on. Alternatively, you can alter the volume via the Control Center by swiping down from the top-right of your screen (iPhone X and later).


Video Answer


1 Answers

You need to use mediaplayer liabrary for it.

just import MediaPlayer

let volumeView = MPVolumeView(frame: CGRect.zero)
self.view.addSubview(volumeView)

You will get event of every press regardless of volume level

like image 102
Jayesh Patel Avatar answered Oct 07 '22 22:10

Jayesh Patel