Created
March 20, 2015 04:09
-
-
Save avioli/1093324040f4818953f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* 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