Skip to content

Commit

Permalink
Define AVAudioSessionCategory constants using raw strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanheise committed Jan 27, 2025
1 parent 7745196 commit ffe5880
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 57 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.1.24

* Add support for SwiftPM.
* Define AVAudioSessionCategory constants using raw strings.

## 0.1.23

Expand Down
42 changes: 10 additions & 32 deletions ios/audio_session/Sources/audio_session/DarwinAudioSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,17 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {

- (void)getCategory:(NSArray *)args result:(FlutterResult)result {
AVAudioSessionCategory category = [[AVAudioSession sharedInstance] category];
result([self categoryToFlutter:category]);
result(category);
}

- (void)setCategory:(NSArray *)args result:(FlutterResult)result {
NSNumber *categoryIndex = (NSNumber *)args[0];
NSString *rawCategory = (NSString *)args[0];
NSNumber *options = (NSNumber *)args[1];
NSNumber *modeIndex = (NSNumber *)args[2];
NSNumber *policyIndex = (NSNumber *)args[3];
NSError *error = nil;
BOOL status;
AVAudioSessionCategory category = [self flutterToCategory:categoryIndex];
AVAudioSessionCategory category = [self rawToCategory:rawCategory];
if (!category) category = AVAudioSessionCategorySoloAmbient;
NSString *mode = [self flutterToMode:modeIndex];
if (!mode) mode = AVAudioSessionModeDefault;
Expand Down Expand Up @@ -153,11 +153,7 @@ - (void)setCategory:(NSArray *)args result:(FlutterResult)result {
- (void)getAvailableCategories:(NSArray *)args result:(FlutterResult)result {
if (@available(iOS 9.0, *)) {
NSArray *categories = [[AVAudioSession sharedInstance] availableCategories];
NSMutableArray *flutterCategories = [NSMutableArray new];
for (int i = 0; i < categories.count; i++) {
[flutterCategories addObject:[self categoryToFlutter:categories[i]]];
}
result(flutterCategories);
result(categories);
} else {
result(@[]);
}
Expand Down Expand Up @@ -244,6 +240,7 @@ - (void)getRecordPermission:(NSArray *)args result:(FlutterResult)result {

- (void)requestRecordPermission:(NSArray *)args result:(FlutterResult)result {
#if AUDIO_SESSION_MICROPHONE
// Deprecated. Replaced by requestRecordPermissionWithCompletionHandler
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
result(@(granted));
}];
Expand Down Expand Up @@ -541,32 +538,13 @@ - (void)setInputGain:(NSArray *)args result:(FlutterResult)result {
}
}

- (AVAudioSessionCategory)flutterToCategory:(NSNumber *)categoryIndex {
AVAudioSessionCategory category = nil;
if (categoryIndex != (id)[NSNull null]) {
switch (categoryIndex.integerValue) {
case 0: category = AVAudioSessionCategoryAmbient; break;
case 1: category = AVAudioSessionCategorySoloAmbient; break;
case 2: category = AVAudioSessionCategoryPlayback; break;
#if AUDIO_SESSION_MICROPHONE
case 3: category = AVAudioSessionCategoryRecord; break;
case 4: category = AVAudioSessionCategoryPlayAndRecord; break;
#endif
case 5: category = AVAudioSessionCategoryMultiRoute; break;
- (AVAudioSessionCategory)rawToCategory:(NSString *)rawCategory {
for (AVAudioSessionCategory category in [[AVAudioSession sharedInstance] availableCategories]) {
if ([category isEqualToString:rawCategory]) {
return category;
}
}
return category;
}

- (NSObject *)categoryToFlutter:(AVAudioSessionCategory)category {
if (category == AVAudioSessionCategoryAmbient) return @(0);
else if (category == AVAudioSessionCategorySoloAmbient) return @(1);
else if (category == AVAudioSessionCategoryPlayback) return @(2);
#if AUDIO_SESSION_MICROPHONE
else if (category == AVAudioSessionCategoryRecord) return @(3);
else if (category == AVAudioSessionCategoryPlayAndRecord) return @(4);
#endif
else return @(5);
return nil;
}

- (NSString *)flutterToMode:(NSNumber *)modeIndex {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,8 @@ class AudioSessionConfiguration {
: this(
avAudioSessionCategory: data['avAudioSessionCategory'] == null
? null
: AVAudioSessionCategory
.values[data['avAudioSessionCategory'] as int],
: AVAudioSessionCategory.fromRawValue(
data['avAudioSessionCategory'] as String),
avAudioSessionCategoryOptions:
data['avAudioSessionCategoryOptions'] == null
? null
Expand Down Expand Up @@ -602,7 +602,7 @@ class AudioSessionConfiguration {

// Converts this instance to JSON.
Map<String, dynamic> toJson() => {
'avAudioSessionCategory': avAudioSessionCategory?.index,
'avAudioSessionCategory': avAudioSessionCategory?.rawValue,
'avAudioSessionCategoryOptions': avAudioSessionCategoryOptions?.value,
'avAudioSessionMode': avAudioSessionMode?.index,
'avAudioSessionRouteSharingPolicy':
Expand Down
44 changes: 31 additions & 13 deletions lib/src/darwin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ class AVAudioSession {

/// (UNTESTED)
Future<AVAudioSessionCategory> get category async {
final index = (await (_channel.invokeMethod<int>('getCategory')))!;
return decodeEnum(AVAudioSessionCategory.values, index,
defaultValue: AVAudioSessionCategory.playback);
final rawCategory = (await (_channel.invokeMethod<String>('getCategory')))!;
return AVAudioSessionCategory.fromRawValue(rawCategory);
}

Future<void> setCategory(
Expand All @@ -99,14 +98,13 @@ class AVAudioSession {
AVAudioSessionRouteSharingPolicy? policy,
]) =>
_channel.invokeMethod('setCategory',
[category?.index, options?.value, mode?.index, policy?.index]);
[category?.rawValue, options?.value, mode?.index, policy?.index]);

/// (UNTESTED)
Future<List<AVAudioSessionCategory>> get availableCategories async =>
(await _channel.invokeMethod<List<dynamic>>('getAvailableCategories'))!
.cast<int>()
.map((index) => decodeEnum(AVAudioSessionCategory.values, index,
defaultValue: AVAudioSessionCategory.playback))
.cast<String>()
.map((rawValue) => AVAudioSessionCategory.fromRawValue(rawValue))
.toList();

/// (UNTESTED)
Expand Down Expand Up @@ -345,12 +343,32 @@ class AVAudioSession {

/// The categories for [AVAudioSession].
enum AVAudioSessionCategory {
ambient,
soloAmbient,
playback,
record,
playAndRecord,
multiRoute,
ambient(_kAmbient),
soloAmbient(_kSoloAmbient),
playback(_kPlayback),
record(_kRecord),
playAndRecord(_kPlayAndRecord),
multiRoute(_kMultiRoute);

static const _kAmbient = 'AVAudioSessionCategoryAmbient';
static const _kSoloAmbient = 'AVAudioSessionCategorySoloAmbient';
static const _kPlayback = 'AVAudioSessionCategoryPlayback';
static const _kRecord = 'AVAudioSessionCategoryRecord';
static const _kPlayAndRecord = 'AVAudioSessionCategoryPlayAndRecord';
static const _kMultiRoute = 'AVAudioSessionCategoryMultiRoute';

static AVAudioSessionCategory fromRawValue(String rawValue) => const {
_kAmbient: ambient,
_kSoloAmbient: soloAmbient,
_kPlayback: playback,
_kRecord: record,
_kPlayAndRecord: playAndRecord,
_kMultiRoute: multiRoute,
}[rawValue]!;

const AVAudioSessionCategory(this.rawValue);

final String rawValue;
}

/// The category options for [AVAudioSession].
Expand Down
18 changes: 9 additions & 9 deletions macos/Classes/AudioSessionPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
- (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
self = [super init];
NSAssert(self, @"super init cannot be nil");
_channel = [FlutterMethodChannel
methodChannelWithName:@"com.ryanheise.audio_session"
_channel = [FlutterMethodChannel
methodChannelWithName:@"com.ryanheise.audio_session"
binaryMessenger:[registrar messenger]];
[registrar addMethodCallDelegate:self channel:_channel];
[registrar addMethodCallDelegate:self channel:_channel];
[plugins addObject:self];
return self;
}
Expand All @@ -31,17 +31,17 @@ - (FlutterMethodChannel *)channel {

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
NSArray* args = (NSArray*)call.arguments;
if ([@"setConfiguration" isEqualToString:call.method]) {
if ([@"setConfiguration" isEqualToString:call.method]) {
configuration = args[0];
for (int i = 0; i < plugins.count; i++) {
[plugins[i].channel invokeMethod:@"onConfigurationChanged" arguments:@[configuration]];
}
result(nil);
} else if ([@"getConfiguration" isEqualToString:call.method]) {
result(nil);
} else if ([@"getConfiguration" isEqualToString:call.method]) {
result(configuration);
} else {
result(FlutterMethodNotImplemented);
}
} else {
result(FlutterMethodNotImplemented);
}
}

- (void) dealloc {
Expand Down

0 comments on commit ffe5880

Please sign in to comment.