iOS7 以上监听音量键Tips
iOS7已经废弃 MPMusicPlayerController 这个类与方法 (还是能使用,但对于以后代码迭代,最好替换掉 ) , 如果需要控制系统音量,需要使用 MPVolumeView
以下是一些使用监听音量变化的Tips
需求:音量键拍照 && 拍照无声黑科技
1 2
| @interface MPVolumeView : UIView <NSCoding>
|
设计思路:
镜头模块需要监测音量键,音量变化触发拍照功能,为此可以设置一个单例来监测
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| +(MPVolumeObserver*) sharedInstance; { static MPVolumeObserver *instance; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[MPVolumeObserver alloc] init]; }); return instance; }
-(id)init { self = [super init]; if( self ){
CGRect frame = CGRectMake(0, -100, 0, 0); _volumeView = [[MPVolumeView alloc] initWithFrame:frame]; [[UIApplication sharedApplication].windows[0] addSubview:_volumeView]; isFirstInit = NO; } return self; }
|
开始监测音量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| -(void) startObserve; {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil]; [[AVAudioSession sharedInstance] setActive:YES error:nil];
[[_volumeView subviews]enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj isKindOfClass:[UISlider class]]) {
[((UISlider*)obj) sendActionsForControlEvents:UIControlEventTouchUpInside]; launchVolume = ((UISlider*)obj).value;
*stop = YES; } }];
launchVolume = launchVolume == 0 ? 0.05 : launchVolume; launchVolume = launchVolume == 1 ? 0.95 : launchVolume; if (launchVolume == 0.05 || launchVolume == 0.95) {
[[_volumeView subviews]enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj isKindOfClass:[UISlider class]]) {
((UISlider*)obj).value = launchVolume; *stop = YES; } }]; }
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(suspendObserveVolumeChangeEvents:) name:UIApplicationWillResignActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resumeObserveVolumeButtonEvents:) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChangeNotification:) name:@"SystemVolumeDidChange" object:nil]; }
|
以上方法能检测出系统音量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| -(void) volumeChangeNotification:(NSNotification *) no { NSLog(@"Volume Change!!"); static id sender = nil; if (sender == nil && no.object) { sender = no.object; } if (no.object != sender || [[no.userInfo objectForKey:@"AudioVolume"] floatValue] == launchVolume) { return; }
if (![[no.userInfo objectForKey:@"AudioVolumeChangeReason"] isEqualToString:@"ExplicitVolumeChange"]) { return; }
MPVolumeView *volumeView= [[MPVolumeView alloc] initWithFrame:CGRectMake(-2000.0f, -2000.0f, 0.0f, 0.0f)]; volumeView.alpha = 0.10f; volumeView.userInteractionEnabled = NO; NSArray *windows = [UIApplication sharedApplication].windows; [[windows firstObject] addSubview:volumeView]; [[volumeView subviews]enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj isKindOfClass:[UISlider class]]) { ((UISlider*)obj).value = launchVolume; *stop = YES; } }]; [volumeView removeFromSuperview];
if ([self.delegate respondsToSelector:@selector(volumeButtonDidClick:)] ) { [self.delegate volumeButtonDidClick:self]; }
}
|
当然需要stop监察
1 2 3 4 5 6 7 8 9 10 11
| -(void)stopObserveVolumeChangeEvents { [[NSNotificationCenter defaultCenter] removeObserver:self];
}
|