System Volume Change Observer not working on iOS 15
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
Detect hardware volume button press when volume not changed
let volumeView = MPVolumeView(frame: CGRect.zero)
self.view.addSubview(volumeView)
NotificationCenter.default.addObserver(self, selector: #selector(volumeChanged(_:)), name: NSNotification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification"), object: nil)
This will get called every press regardless of volume level
@objc func volumeChanged(_ notification: NSNotification) {
if let volume = notification.userInfo!["AVSystemController_AudioVolumeNotificationParameter"] as? Float {
print("volume: \(volume)")
}
}
output:
volume: 0.8125
volume: 0.875
volume: 0.9375
volume: 1.0
volume: 1.0
volume: 1.0
volume: 1.0
get current system volume level on iPhone
musicPlayer = [[MPMusicPlayerController iPodMusicPlayer];
currentVolume = musicPlayer.volume;
This is now deprecated as of iOS8.0 so try the following
#import
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
CGFloat volume = audioSession.outputVolume;
Related Topics
Undefined Symbols for Architecture X86_64 on Xcode 6.1
iOS Memory Allocation - How Much Memory Can Be Used in an Application
Counting the Number of Lines in a Uitextview, Lines Wrapped by Frame Size
Restrict to Certain iOS Target Devices for App Store Submission
How to Create a Centered Uicollectionview Like in Spotify's Player
Change Status Bar Background Color in Swift 3
Core Data - Failed to Load Optimized Model at Path
Unique Identification of iOS Device for iOS 7.0 and Above
How to Create an Empty Application in Xcode Without Storyboard
Ibeacon: Didrangebeacons Stops Getting Called, Must Reset Device for It to Work Again
Remove Gradient Background from Uiwebview
Get Progress from Datataskwithurl in Swift
How to Find File Uti for File, Withouth Pathextension, in a Path in Swift
Make a Uibutton Programmatically in Swift