Skip to content

Instantly share code, notes, and snippets.

@avioli
Created March 20, 2015 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avioli/1093324040f4818953f1 to your computer and use it in GitHub Desktop.
Save avioli/1093324040f4818953f1 to your computer and use it in GitHub Desktop.
/*!
* Do not use in production app.
*/
#if DEBUG
#import <MediaPlayer/MediaPlayer.h>
#endif
- (void)addVolumeControlObserverWithWindow:(UIWindow *)window {
#if DEBUG
MPVolumeView *volume = [[MPVolumeView alloc] initWithFrame:CGRectZero];
[window addSubview:volume];
UISlider *volumeViewSlider;
for (UIView *view in [volumeViewSlider subviews]){
if ([[[view class] description] isEqualToString:@"MPVolumeSlider"]) {
volumeViewSlider = (UISlider *) view;
}
}
// Get volume level of the current output device (speakers, headphones, etc.)
static float val = 0.0;
val = [volumeViewSlider value];
// Register to receive the button-press notifications
__block id volumeObserver;
volumeObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil queue:nil usingBlock:^(NSNotification *note)
{
float newVal = [[note.userInfo valueForKey:@"AVSystemController_AudioVolumeNotificationParameter"] floatValue];
if (newVal >= 1.0) {
NSLog(@"Up MAX");
} else if (newVal <= 0.0625) {
NSLog(@"Down MIN");
} else if (newVal > val) {
NSLog(@"Up");
} else if (newVal < val) {
NSLog(@"Down");
} else {
// should never happen
NSLog(@"Same");
}
val = newVal;
}];
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment