KSAudioSessionManager.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // KSAudioSessionManager.m
  3. // TeacherDaya
  4. //
  5. // Created by Kyle on 2021/6/29.
  6. // Copyright © 2021 DayaMusic. All rights reserved.
  7. //
  8. #import "KSAudioSessionManager.h"
  9. #import <AVFoundation/AVFoundation.h>
  10. @interface KSAudioSessionManager ()
  11. @end
  12. @implementation KSAudioSessionManager
  13. - (instancetype)init {
  14. self = [super init];
  15. if (self) {
  16. AVAudioSession *session = [AVAudioSession sharedInstance];
  17. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterruption:) name:AVAudioSessionInterruptionNotification object:session];
  18. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(routeChangeNotification:) name:AVAudioSessionRouteChangeNotification object:session];
  19. }
  20. return self;
  21. }
  22. - (void)routeChangeNotification:(NSNotification *)notification {
  23. NSDictionary *info = notification.userInfo;
  24. AVAudioSessionRouteChangeReason routeChangeReason = [[info valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
  25. switch (routeChangeReason) {
  26. case AVAudioSessionRouteChangeReasonNewDeviceAvailable: // 新设备接入
  27. {
  28. [self queryOutputDevice];
  29. }
  30. break;
  31. case AVAudioSessionRouteChangeReasonOldDeviceUnavailable: // 旧设备不可用
  32. {
  33. [self queryOutputDevice];
  34. }
  35. break;
  36. case AVAudioSessionRouteChangeReasonCategoryChange:
  37. {
  38. }
  39. break;
  40. case AVAudioSessionRouteChangeReasonOverride:
  41. {
  42. }
  43. break;
  44. case AVAudioSessionRouteChangeReasonUnknown:
  45. {
  46. }
  47. break;
  48. default:
  49. break;
  50. }
  51. }
  52. - (void)queryOutputDevice {
  53. AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
  54. AUDIODEVICE_TYPE type = AUDIODEVICE_TYPE_NONE;
  55. for (AVAudioSessionPortDescription* desc in [route outputs]) {
  56. NSString *protType = [desc portType];
  57. NSLog(@"-- prot type %@", protType);
  58. if ([protType isEqualToString:AVAudioSessionPortHeadphones] || [protType isEqualToString:AVAudioSessionPortUSBAudio]) {
  59. type = AUDIODEVICE_TYPE_HEADPHONE;
  60. break;
  61. }
  62. else if ([protType isEqualToString:AVAudioSessionPortBluetoothA2DP] || [protType isEqualToString:AVAudioSessionPortBluetoothHFP] || [protType isEqualToString:AVAudioSessionPortBluetoothLE]) {
  63. type = AUDIODEVICE_TYPE_BLUETOOTH;
  64. break;
  65. }
  66. }
  67. if (self.delegate && [self.delegate respondsToSelector:@selector(audioRouteChange:)]) {
  68. [self.delegate audioRouteChange:type];
  69. }
  70. }
  71. + (AUDIODEVICE_TYPE)queryAudioOutputDevice {
  72. AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
  73. for (AVAudioSessionPortDescription* desc in [route outputs]) {
  74. NSString *protType = [desc portType];
  75. if ([protType isEqualToString:AVAudioSessionPortHeadphones] || [protType isEqualToString:AVAudioSessionPortUSBAudio]) {
  76. return AUDIODEVICE_TYPE_HEADPHONE;
  77. }
  78. else if ([protType isEqualToString:AVAudioSessionPortBluetoothA2DP] || [protType isEqualToString:AVAudioSessionPortBluetoothHFP] || [protType isEqualToString:AVAudioSessionPortBluetoothLE]) {
  79. return AUDIODEVICE_TYPE_BLUETOOTH;
  80. }
  81. }
  82. return AUDIODEVICE_TYPE_NONE;
  83. }
  84. // 打断处理
  85. - (void)handleInterruption:(NSNotification *)notification {
  86. NSDictionary *info = notification.userInfo;
  87. AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
  88. if (type == AVAudioSessionInterruptionTypeBegan) {
  89. //Handle InterruptionBegan
  90. if (self.delegate && [self.delegate respondsToSelector:@selector(recordInterruption)]) {
  91. [self.delegate recordInterruption];
  92. }
  93. }else{
  94. AVAudioSessionInterruptionOptions options = [info[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
  95. if (options == AVAudioSessionInterruptionOptionShouldResume) {
  96. //Handle Resume
  97. if (self.delegate && [self.delegate respondsToSelector:@selector(resumeAudioSession)]) {
  98. [self.delegate resumeAudioSession];
  99. }
  100. }
  101. }
  102. }
  103. - (void)configAudioSession:(AUDIOCONFIG)config {
  104. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  105. NSError *err = nil;
  106. [audioSession setActive:NO error:&err];
  107. AVAudioSessionCategory category = AVAudioSessionCategoryPlayAndRecord;
  108. switch (config) {
  109. case AUDIOCONFIG_PLAY:
  110. {
  111. category = AVAudioSessionCategoryPlayback;
  112. }
  113. break;
  114. case AUDIOCONFIG_RECORD:
  115. {
  116. category = AVAudioSessionCategoryRecord;
  117. }
  118. break;
  119. case AUDIOCONFIG_PLAYANDRECORD:
  120. {
  121. category = AVAudioSessionCategoryPlayAndRecord;
  122. }
  123. break;
  124. default:
  125. category = AVAudioSessionCategoryPlayAndRecord;
  126. break;
  127. }
  128. if (@available(iOS 10.0, *)) {
  129. [audioSession setCategory:category mode:AVAudioSessionModeDefault options:AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionAllowBluetoothA2DP|AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers error:&err];
  130. }
  131. else {
  132. [audioSession setCategory:category withOptions:AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers error:&err];
  133. }
  134. [audioSession setActive:YES error:&err];
  135. }
  136. // 切换到手机麦克风 ,会导致扬声器同步切换
  137. - (void)configMicWithSession:(AVAudioSession *)audioSession {
  138. NSArray *inputs = [audioSession availableInputs];
  139. NSLog(@"%@", inputs.description);
  140. AVAudioSessionPortDescription *builtInMic = nil;
  141. for (AVAudioSessionPortDescription *port in inputs) {
  142. if ([port.portType isEqualToString:AVAudioSessionPortBuiltInMic]) {
  143. builtInMic = port;
  144. break;
  145. }
  146. }
  147. for (AVAudioSessionDataSourceDescription *source in builtInMic.dataSources) {
  148. if ([source.orientation isEqualToString:AVAudioSessionOrientationFront]) {
  149. [builtInMic setPreferredDataSource:source error:nil];
  150. [audioSession setPreferredInput:builtInMic error:nil];
  151. break;
  152. }
  153. }
  154. }
  155. - (void)dealloc {
  156. [[NSNotificationCenter defaultCenter] removeObserver:self];
  157. }
  158. @end