FUVolumeObserver.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // FUVolumeObserver.m
  3. // FULiveDemo
  4. //
  5. // Created by 孙慕 on 2018/10/22.
  6. // Copyright © 2018年 L. All rights reserved.
  7. //
  8. // 音量按钮劫持
  9. #import "FUVolumeObserver.h"
  10. #import <AVFoundation/AVFoundation.h>
  11. @interface FUVolumeObserver()
  12. {
  13. UIView *_volumeView;
  14. float launchVolume;
  15. BOOL isObserving;
  16. }
  17. @end
  18. @implementation FUVolumeObserver
  19. + (FUVolumeObserver*) sharedInstance;
  20. {
  21. static FUVolumeObserver *instance;
  22. static dispatch_once_t onceToken;
  23. dispatch_once(&onceToken, ^{
  24. instance = [[FUVolumeObserver alloc] init];
  25. });
  26. return instance;
  27. }
  28. - (id) init
  29. {
  30. self = [super init];
  31. if( self ){
  32. isObserving = NO;
  33. CGRect frame = CGRectMake(0, -100, 0, 0);
  34. _volumeView = [[MPVolumeView alloc] initWithFrame:frame];
  35. [[UIApplication sharedApplication].windows[0] addSubview:_volumeView];
  36. [[NSNotificationCenter defaultCenter] addObserver:self
  37. selector:@selector(suspendObserveVolumeChangeEvents:)name:UIApplicationWillResignActiveNotification // -> Inactive
  38. object:nil];
  39. [[NSNotificationCenter defaultCenter] addObserver:self
  40. selector:@selector(resumeObserveVolumeButtonEvents:)name:UIApplicationDidBecomeActiveNotification // <- Active
  41. object:nil];
  42. }
  43. return self;
  44. }
  45. - (void) startObserveVolumeChangeEvents{
  46. [[UIApplication sharedApplication].windows[0] addSubview:_volumeView];
  47. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  48. launchVolume = audioSession.outputVolume;
  49. launchVolume = launchVolume == 0 ? 0.05 : launchVolume;
  50. launchVolume = launchVolume == 1 ? 0.95 : launchVolume;
  51. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{//防止进程中的回调接收
  52. [self startObserve];
  53. });
  54. }
  55. - (void) startObserve
  56. {
  57. if (isObserving) {
  58. return;
  59. }
  60. isObserving = YES;
  61. AudioSessionInitialize(NULL, NULL, NULL, NULL);
  62. SInt32 process = kAudioSessionCategory_AmbientSound;
  63. AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(process), &process);
  64. AudioSessionSetActive(YES);
  65. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChangeNotification:) name:@"SystemVolumeDidChange" object:nil];
  66. }
  67. - (void) volumeChangeNotification:(NSNotification *) no
  68. {
  69. static id sender = nil;
  70. if (sender == nil && no.object) {
  71. sender = no.object;
  72. }
  73. float val = [[no.userInfo objectForKey:@"AudioVolume"] floatValue];
  74. if (no.object != sender) {
  75. return;
  76. }
  77. if(!_delegate) return;
  78. if (!isObserving) {
  79. return;
  80. }
  81. if (val > launchVolume) {
  82. if ([_delegate respondsToSelector:@selector(volumeButtonDidUp:)]) {
  83. [_delegate volumeButtonDidUp:self];
  84. }
  85. [[MPMusicPlayerController applicationMusicPlayer] setVolume:launchVolume];
  86. } else if (val < launchVolume) {
  87. if ([_delegate respondsToSelector:@selector(volumeButtonDidDown:)]) {
  88. [_delegate volumeButtonDidDown:self];
  89. }
  90. [[MPMusicPlayerController applicationMusicPlayer] setVolume:launchVolume];
  91. }
  92. }
  93. - (void) suspendObserveVolumeChangeEvents:(NSNotification *)notification
  94. {
  95. [self stopObserveVolumeChangeEvents];
  96. }
  97. - (void) resumeObserveVolumeButtonEvents:(NSNotification *)notification
  98. {
  99. [self startObserveVolumeChangeEvents];
  100. }
  101. - (void) stopObserveVolumeChangeEvents
  102. {
  103. isObserving = NO;
  104. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"SystemVolumeDidChange" object:nil];
  105. AudioSessionRemovePropertyListenerWithUserData(kAudioSessionProperty_CurrentHardwareOutputVolume, NULL, (__bridge void *)(self));
  106. AudioSessionSetActive(NO);
  107. [_volumeView removeFromSuperview];
  108. }
  109. -(void)dealloc{
  110. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
  111. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
  112. }
  113. @end