-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ios): implement AudioSession utils
- Loading branch information
Showing
4 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#import <Foundation/Foundation.h> | ||
#import <AVFoundation/AVFoundation.h> | ||
|
||
@interface RNWhisperAudioSessionUtils : NSObject | ||
|
||
+(NSString *)getCurrentCategory; | ||
+(NSArray *)getCurrentOptions; | ||
+(NSString *)getCurrentMode; | ||
+(void)setCategory:(NSString *)category options:(NSArray *)options error:(NSError **)error; | ||
+(void)setMode:(NSString *)mode error:(NSError **)error; | ||
+(void)setActive:(BOOL)active error:(NSError **)error; | ||
|
||
@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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#import "RNWhisperAudioSessionUtils.h" | ||
|
||
@implementation RNWhisperAudioSessionUtils | ||
|
||
static NSDictionary *_categories; | ||
static NSDictionary *_options; | ||
static NSDictionary *_modes; | ||
|
||
+ (void)initialize { | ||
_categories = @{ | ||
@"Ambient": AVAudioSessionCategoryAmbient, | ||
@"SoloAmbient": AVAudioSessionCategorySoloAmbient, | ||
@"Playback": AVAudioSessionCategoryPlayback, | ||
@"Record": AVAudioSessionCategoryRecord, | ||
@"PlayAndRecord": AVAudioSessionCategoryPlayAndRecord, | ||
@"MultiRoute": AVAudioSessionCategoryMultiRoute | ||
}; | ||
_options = @{ | ||
@"MixWithOthers": @(AVAudioSessionCategoryOptionMixWithOthers), | ||
@"DuckOthers": @(AVAudioSessionCategoryOptionDuckOthers), | ||
@"InterruptSpokenAudioAndMixWithOthers": @(AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers), | ||
@"AllowBluetooth": @(AVAudioSessionCategoryOptionAllowBluetooth), | ||
@"AllowBluetoothA2DP": @(AVAudioSessionCategoryOptionAllowBluetoothA2DP), | ||
@"AllowAirPlay": @(AVAudioSessionCategoryOptionAllowAirPlay), | ||
@"DefaultToSpeaker": @(AVAudioSessionCategoryOptionDefaultToSpeaker) | ||
}; | ||
_modes = @{ | ||
@"Default": AVAudioSessionModeDefault, | ||
@"VoiceChat": AVAudioSessionModeVoiceChat, | ||
@"VideoChat": AVAudioSessionModeVideoChat, | ||
@"GameChat": AVAudioSessionModeGameChat, | ||
@"VideoRecording": AVAudioSessionModeVideoRecording, | ||
@"Measurement": AVAudioSessionModeMeasurement, | ||
@"MoviePlayback": AVAudioSessionModeMoviePlayback, | ||
@"SpokenAudio": AVAudioSessionModeSpokenAudio | ||
}; | ||
} | ||
|
||
+(NSString *)getCurrentCategory { | ||
AVAudioSession *session = [AVAudioSession sharedInstance]; | ||
return session.category; | ||
} | ||
|
||
+(NSArray *)getCurrentOptions { | ||
AVAudioSession *session = [AVAudioSession sharedInstance]; | ||
AVAudioSessionCategoryOptions options = session.categoryOptions; | ||
NSMutableArray *result = [NSMutableArray array]; | ||
for (NSString *key in _options) { | ||
if ((options & [[_options objectForKey:key] unsignedIntegerValue]) != 0) { | ||
[result addObject:key]; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
+(NSString *)getCurrentMode { | ||
AVAudioSession *session = [AVAudioSession sharedInstance]; | ||
return session.mode; | ||
} | ||
|
||
+(void)setCategory:(NSString *)category options:(NSArray *)options error:(NSError **)error { | ||
AVAudioSession *session = [AVAudioSession sharedInstance]; | ||
[session setCategory:[_categories objectForKey:category] withOptions:[self getOptions:options] error:error]; | ||
} | ||
|
||
+(void)setMode:(NSString *)mode error:(NSError **)error { | ||
AVAudioSession *session = [AVAudioSession sharedInstance]; | ||
[session setMode:[_modes objectForKey:mode] error:error]; | ||
} | ||
|
||
+(void)setActive:(BOOL)active error:(NSError **)error { | ||
AVAudioSession *session = [AVAudioSession sharedInstance]; | ||
[session setActive:active error:error]; | ||
} | ||
|
||
+(AVAudioSessionCategoryOptions)getOptions:(NSArray *)options { | ||
AVAudioSessionCategoryOptions result = 0; | ||
for (NSString *option in options) { | ||
result |= [[_options objectForKey:option] unsignedIntegerValue]; | ||
} | ||
return result; | ||
} | ||
|
||
@end |