#import <AVFoundation/AVFoundation.h>
- (void)addVolumeObserver
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error = nil;
[audioSession setActive:YES error:&error];
if (error) {
NSLog(@"%@", error);
}
[audioSession addObserver:self forKeyPath:@"outputVolume" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
if (![keyPath isEqualToString:@"outputVolume"]) {
return;
}
NSKeyValueChangeKey newValue = change[@"new"];
NSKeyValueChangeKey oldValue = change[@"old"];
NSLog(@"oldValue=%@ newValue=%@", oldValue, newValue);
}