Created
April 4, 2017 09:22
-
-
Save Shebuka/db7839183b837f7541a859311abe11f6 to your computer and use it in GitHub Desktop.
AVAudioSession extension for easy switch between categories and session activation and deactivation with error check and logging.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AVAudioSession+Switch.h | |
// | |
// Created by Anton Shebukov on 09/06/16. | |
// Copyright (c) 2013-2017 Feedback Italia S.r.l. All rights reserved. | |
// | |
#import <AVFoundation/AVFoundation.h> | |
@interface AVAudioSession (SwitchAddition) | |
- (BOOL)switchToPlayAndRecordCategory; | |
- (BOOL)switchToPlaybackCategory; | |
- (BOOL)switchToRecordCategory; | |
- (BOOL)setMaxInputGain; | |
- (BOOL)activate; | |
- (BOOL)deactiveAndNotifyOthers; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AVAudioSession+Switch.m | |
// | |
// Created by Anton Shebukov on 09/06/16. | |
// Copyright (c) 2013-2017 Feedback Italia S.r.l. All rights reserved. | |
// | |
#import "AVAudioSession+Switch.h" | |
#import "GTMLogger.h" | |
@implementation AVAudioSession (SwitchAddition) | |
- (BOOL)switchToPlayAndRecordCategory { | |
[[GTMLogger sharedLogger] logDebug:@"AVAudioSession::switchToPlayAndRecordCategory"]; | |
NSError *error = nil; | |
[self setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionAllowBluetoothA2DP error:&error]; | |
if (error) { | |
[[GTMLogger sharedLogger] logError:@"AVAudioSession::switchToPlayAndRecordCategory setCategory: %@ %ld %@", [error domain], (long)[error code], [[error userInfo] description]]; | |
return NO; | |
} | |
return YES; | |
} | |
- (BOOL)switchToPlaybackCategory { | |
[[GTMLogger sharedLogger] logDebug:@"AVAudioSession::switchToPlaybackCategory"]; | |
NSError *error = nil; | |
[self setCategory:AVAudioSessionCategoryPlayback withOptions:0 error:&error]; | |
if (error) { | |
[[GTMLogger sharedLogger] logError:@"AVAudioSession::switchToPlaybackCategory setCategory: %@ %ld %@", [error domain], (long)[error code], [[error userInfo] description]]; | |
return NO; | |
} | |
return YES; | |
} | |
- (BOOL)switchToRecordCategory { | |
[[GTMLogger sharedLogger] logDebug:@"AVAudioSession::switchToRecordCategory"]; | |
NSError *error = nil; | |
[self setCategory:AVAudioSessionCategoryRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:&error]; | |
if (error) { | |
[[GTMLogger sharedLogger] logError:@"AVAudioSession::switchToRecordCategory setCategory: %@ %ld %@", [error domain], (long)[error code], [[error userInfo] description]]; | |
return NO; | |
} | |
[self setMode:AVAudioSessionModeVideoRecording error:&error]; | |
if (error) { | |
[[GTMLogger sharedLogger] logError:@"AVAudioSession::switchToRecordCategory setMode: %@ %ld %@", [error domain], (long)[error code], [[error userInfo] description]]; | |
return NO; | |
} | |
return YES; | |
} | |
- (BOOL)setMaxInputGain { | |
[[GTMLogger sharedLogger] logDebug:@"AVAudioSession::setMaxInputGain"]; | |
NSError *error = nil; | |
if (![self isInputAvailable]) { | |
[[GTMLogger sharedLogger] logError:@"AVAudioSession::setMaxInputGain not isInputAvailable"]; | |
return NO; | |
} | |
if (![self isInputGainSettable]) { | |
[[GTMLogger sharedLogger] logError:@"AVAudioSession::setMaxInputGain not isInputGainSettable"]; | |
return NO; | |
} | |
[self setInputGain:1.0 error:&error]; | |
if (error) { | |
[[GTMLogger sharedLogger] logError:@"AVAudioSession::setMaxInputGain setInputGain: %@ %ld %@", [error domain], (long)[error code], [[error userInfo] description]]; | |
return NO; | |
} | |
return YES; | |
} | |
- (BOOL)activate { | |
[[GTMLogger sharedLogger] logDebug:@"AVAudioSession::activate"]; | |
NSError *error = nil; | |
[self setActive:YES withOptions:0 error:&error]; | |
if (error) { | |
[[GTMLogger sharedLogger] logError:@"AVAudioSession::activate setActive: %@ %ld %@", [error domain], (long)[error code], [[error userInfo] description]]; | |
return NO; | |
} | |
return YES; | |
} | |
- (BOOL)deactiveAndNotifyOthers { | |
[[GTMLogger sharedLogger] logDebug:@"AVAudioSession::deactiveAndNotifyOthers"]; | |
NSError *error = nil; | |
[self setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error]; | |
if (error) { | |
[[GTMLogger sharedLogger] logError:@"AVAudioSession::deactiveAndNotifyOthers setActive: %@ %ld %@", [error domain], (long)[error code], [[error userInfo] description]]; | |
return NO; | |
} | |
return YES; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment