How can I detect volume change while screen is off in iOS?

640 views Asked by At

I have code that detects volume change when the app is running in the foreground. But I want to detect volume change in the background and when the screen is off.

AVAudioSession* audioSession = [AVAudioSession sharedInstance];        
[audioSession setActive:YES error:nil]; // Start Audio Session
[audioSession addObserver:self  //observer for the audio session
               forKeyPath:@"outputVolume"
                  options:0
                  context:nil];
}

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    if ([keyPath isEqual:@"outputVolume"]) {
        NSLog(@"volume changed!");
    }
}

Here is my log output when I press volume while app is in foreground.

2015-06-23 08:54:44.523 MayDay[469:71914] volume changed!
0

There are 0 answers