ZFPlayerNotification.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // ZFPlayerNotification.m
  3. // ZFPlayer
  4. //
  5. // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import "ZFPlayerNotification.h"
  25. @interface ZFPlayerNotification ()
  26. @property (nonatomic, assign) ZFPlayerBackgroundState backgroundState;
  27. @end
  28. @implementation ZFPlayerNotification
  29. - (void)addNotification {
  30. [[NSNotificationCenter defaultCenter] addObserver:self
  31. selector:@selector(audioSessionRouteChangeNotification:)
  32. name:AVAudioSessionRouteChangeNotification
  33. object:nil];
  34. [[NSNotificationCenter defaultCenter] addObserver:self
  35. selector:@selector(applicationWillResignActiveNotification)
  36. name:UIApplicationWillResignActiveNotification
  37. object:nil];
  38. [[NSNotificationCenter defaultCenter] addObserver:self
  39. selector:@selector(applicationDidBecomeActiveNotification)
  40. name:UIApplicationDidBecomeActiveNotification
  41. object:nil];
  42. [[NSNotificationCenter defaultCenter] addObserver:self
  43. selector:@selector(volumeDidChangeNotification:)
  44. name:@"AVSystemController_SystemVolumeDidChangeNotification"
  45. object:nil];
  46. [[NSNotificationCenter defaultCenter] addObserver:self
  47. selector:@selector(audioSessionInterruptionNotification:)
  48. name:AVAudioSessionInterruptionNotification
  49. object:nil];
  50. }
  51. - (void)removeNotification {
  52. [[NSNotificationCenter defaultCenter] removeObserver:self name:AVAudioSessionRouteChangeNotification object:nil];
  53. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
  54. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
  55. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
  56. [[NSNotificationCenter defaultCenter] removeObserver:self name:AVAudioSessionInterruptionNotification object:nil];
  57. }
  58. - (void)dealloc {
  59. [self removeNotification];
  60. }
  61. - (void)audioSessionRouteChangeNotification:(NSNotification*)notification {
  62. dispatch_async(dispatch_get_main_queue(), ^{
  63. NSDictionary *interuptionDict = notification.userInfo;
  64. NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
  65. switch (routeChangeReason) {
  66. case AVAudioSessionRouteChangeReasonNewDeviceAvailable: {
  67. if (self.newDeviceAvailable) self.newDeviceAvailable(self);
  68. }
  69. break;
  70. case AVAudioSessionRouteChangeReasonOldDeviceUnavailable: {
  71. if (self.oldDeviceUnavailable) self.oldDeviceUnavailable(self);
  72. }
  73. break;
  74. case AVAudioSessionRouteChangeReasonCategoryChange: {
  75. if (self.categoryChange) self.categoryChange(self);
  76. }
  77. break;
  78. }
  79. });
  80. }
  81. - (void)volumeDidChangeNotification:(NSNotification *)notification {
  82. float volume = [[[notification userInfo] objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"] floatValue];
  83. if (self.volumeChanged) self.volumeChanged(volume);
  84. }
  85. - (void)applicationWillResignActiveNotification {
  86. self.backgroundState = ZFPlayerBackgroundStateBackground;
  87. if (_willResignActive) _willResignActive(self);
  88. }
  89. - (void)applicationDidBecomeActiveNotification {
  90. self.backgroundState = ZFPlayerBackgroundStateForeground;
  91. if (_didBecomeActive) _didBecomeActive(self);
  92. }
  93. - (void)audioSessionInterruptionNotification:(NSNotification *)notification {
  94. NSDictionary *interuptionDict = notification.userInfo;
  95. AVAudioSessionInterruptionType interruptionType = [[interuptionDict valueForKey:AVAudioSessionInterruptionTypeKey] integerValue];
  96. if (self.audioInterruptionCallback) self.audioInterruptionCallback(interruptionType);
  97. }
  98. @end