123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- //
- // KSAudioSessionManager.m
- // TeacherDaya
- //
- // Created by Kyle on 2021/6/29.
- // Copyright © 2021 DayaMusic. All rights reserved.
- //
- #import "KSAudioSessionManager.h"
- #import <AVFoundation/AVFoundation.h>
- @interface KSAudioSessionManager ()
- @end
- @implementation KSAudioSessionManager
- - (instancetype)init {
- self = [super init];
- if (self) {
- AVAudioSession *session = [AVAudioSession sharedInstance];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterruption:) name:AVAudioSessionInterruptionNotification object:session];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(routeChangeNotification:) name:AVAudioSessionRouteChangeNotification object:session];
- }
- return self;
- }
- - (void)routeChangeNotification:(NSNotification *)notification {
- NSDictionary *info = notification.userInfo;
- AVAudioSessionRouteChangeReason routeChangeReason = [[info valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
- switch (routeChangeReason) {
- case AVAudioSessionRouteChangeReasonNewDeviceAvailable: // 新设备接入
- {
- [self queryOutputDevice];
- }
- break;
- case AVAudioSessionRouteChangeReasonOldDeviceUnavailable: // 旧设备不可用
- {
- [self queryOutputDevice];
- }
- break;
- case AVAudioSessionRouteChangeReasonCategoryChange:
- {
- }
- break;
- case AVAudioSessionRouteChangeReasonOverride:
- {
- }
- break;
- case AVAudioSessionRouteChangeReasonUnknown:
- {
- }
- break;
- default:
- break;
- }
- }
- - (void)queryOutputDevice {
- AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
- AUDIODEVICE_TYPE type = AUDIODEVICE_TYPE_NONE;
-
- for (AVAudioSessionPortDescription* desc in [route outputs]) {
- NSString *protType = [desc portType];
- NSLog(@"-- prot type %@", protType);
- if ([protType isEqualToString:AVAudioSessionPortHeadphones] || [protType isEqualToString:AVAudioSessionPortUSBAudio]) {
- type = AUDIODEVICE_TYPE_HEADPHONE;
- break;
- }
- else if ([protType isEqualToString:AVAudioSessionPortBluetoothA2DP] || [protType isEqualToString:AVAudioSessionPortBluetoothHFP] || [protType isEqualToString:AVAudioSessionPortBluetoothLE]) {
- type = AUDIODEVICE_TYPE_BLUETOOTH;
- break;
- }
- }
- if (self.delegate && [self.delegate respondsToSelector:@selector(audioRouteChange:)]) {
- [self.delegate audioRouteChange:type];
- }
- }
- + (AUDIODEVICE_TYPE)queryAudioOutputDevice {
- AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
- for (AVAudioSessionPortDescription* desc in [route outputs]) {
- NSString *protType = [desc portType];
- if ([protType isEqualToString:AVAudioSessionPortHeadphones] || [protType isEqualToString:AVAudioSessionPortUSBAudio]) {
- return AUDIODEVICE_TYPE_HEADPHONE;
- }
- else if ([protType isEqualToString:AVAudioSessionPortBluetoothA2DP] || [protType isEqualToString:AVAudioSessionPortBluetoothHFP] || [protType isEqualToString:AVAudioSessionPortBluetoothLE]) {
- return AUDIODEVICE_TYPE_BLUETOOTH;
- }
- }
- return AUDIODEVICE_TYPE_NONE;
- }
- // 打断处理
- - (void)handleInterruption:(NSNotification *)notification {
-
- NSDictionary *info = notification.userInfo;
- AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
- if (type == AVAudioSessionInterruptionTypeBegan) {
- //Handle InterruptionBegan
- if (self.delegate && [self.delegate respondsToSelector:@selector(recordInterruption)]) {
- [self.delegate recordInterruption];
- }
- }else{
- AVAudioSessionInterruptionOptions options = [info[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
- if (options == AVAudioSessionInterruptionOptionShouldResume) {
- //Handle Resume
- if (self.delegate && [self.delegate respondsToSelector:@selector(resumeAudioSession)]) {
- [self.delegate resumeAudioSession];
- }
- }
- }
- }
- - (void)configAudioSession:(AUDIOCONFIG)config {
- AVAudioSession *audioSession = [AVAudioSession sharedInstance];
- NSError *err = nil;
- [audioSession setActive:NO error:&err];
-
- AVAudioSessionCategory category = AVAudioSessionCategoryPlayAndRecord;
-
- switch (config) {
- case AUDIOCONFIG_PLAY:
- {
- category = AVAudioSessionCategoryPlayback;
- }
- break;
- case AUDIOCONFIG_RECORD:
- {
- category = AVAudioSessionCategoryRecord;
- }
- break;
- case AUDIOCONFIG_PLAYANDRECORD:
- {
- category = AVAudioSessionCategoryPlayAndRecord;
- }
- break;
- default:
- category = AVAudioSessionCategoryPlayAndRecord;
- break;
- }
-
- if (@available(iOS 10.0, *)) {
- [audioSession setCategory:category mode:AVAudioSessionModeDefault options:AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionAllowBluetoothA2DP|AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers error:&err];
- }
- else {
- [audioSession setCategory:category withOptions:AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers error:&err];
- }
-
- [audioSession setActive:YES error:&err];
-
- }
- // 切换到手机麦克风 ,会导致扬声器同步切换
- - (void)configMicWithSession:(AVAudioSession *)audioSession {
-
- NSArray *inputs = [audioSession availableInputs];
- NSLog(@"%@", inputs.description);
- AVAudioSessionPortDescription *builtInMic = nil;
- for (AVAudioSessionPortDescription *port in inputs) {
- if ([port.portType isEqualToString:AVAudioSessionPortBuiltInMic]) {
- builtInMic = port;
- break;
- }
- }
- for (AVAudioSessionDataSourceDescription *source in builtInMic.dataSources) {
- if ([source.orientation isEqualToString:AVAudioSessionOrientationFront]) {
- [builtInMic setPreferredDataSource:source error:nil];
- [audioSession setPreferredInput:builtInMic error:nil];
- break;
- }
- }
- }
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- @end
|