Skip to content

Commit ffe5880

Browse files
committed
Define AVAudioSessionCategory constants using raw strings.
1 parent 7745196 commit ffe5880

5 files changed

Lines changed: 54 additions & 57 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 0.1.24
22

33
* Add support for SwiftPM.
4+
* Define AVAudioSessionCategory constants using raw strings.
45

56
## 0.1.23
67

ios/audio_session/Sources/audio_session/DarwinAudioSession.m

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,17 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
104104

105105
- (void)getCategory:(NSArray *)args result:(FlutterResult)result {
106106
AVAudioSessionCategory category = [[AVAudioSession sharedInstance] category];
107-
result([self categoryToFlutter:category]);
107+
result(category);
108108
}
109109

110110
- (void)setCategory:(NSArray *)args result:(FlutterResult)result {
111-
NSNumber *categoryIndex = (NSNumber *)args[0];
111+
NSString *rawCategory = (NSString *)args[0];
112112
NSNumber *options = (NSNumber *)args[1];
113113
NSNumber *modeIndex = (NSNumber *)args[2];
114114
NSNumber *policyIndex = (NSNumber *)args[3];
115115
NSError *error = nil;
116116
BOOL status;
117-
AVAudioSessionCategory category = [self flutterToCategory:categoryIndex];
117+
AVAudioSessionCategory category = [self rawToCategory:rawCategory];
118118
if (!category) category = AVAudioSessionCategorySoloAmbient;
119119
NSString *mode = [self flutterToMode:modeIndex];
120120
if (!mode) mode = AVAudioSessionModeDefault;
@@ -153,11 +153,7 @@ - (void)setCategory:(NSArray *)args result:(FlutterResult)result {
153153
- (void)getAvailableCategories:(NSArray *)args result:(FlutterResult)result {
154154
if (@available(iOS 9.0, *)) {
155155
NSArray *categories = [[AVAudioSession sharedInstance] availableCategories];
156-
NSMutableArray *flutterCategories = [NSMutableArray new];
157-
for (int i = 0; i < categories.count; i++) {
158-
[flutterCategories addObject:[self categoryToFlutter:categories[i]]];
159-
}
160-
result(flutterCategories);
156+
result(categories);
161157
} else {
162158
result(@[]);
163159
}
@@ -244,6 +240,7 @@ - (void)getRecordPermission:(NSArray *)args result:(FlutterResult)result {
244240

245241
- (void)requestRecordPermission:(NSArray *)args result:(FlutterResult)result {
246242
#if AUDIO_SESSION_MICROPHONE
243+
// Deprecated. Replaced by requestRecordPermissionWithCompletionHandler
247244
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
248245
result(@(granted));
249246
}];
@@ -541,32 +538,13 @@ - (void)setInputGain:(NSArray *)args result:(FlutterResult)result {
541538
}
542539
}
543540

544-
- (AVAudioSessionCategory)flutterToCategory:(NSNumber *)categoryIndex {
545-
AVAudioSessionCategory category = nil;
546-
if (categoryIndex != (id)[NSNull null]) {
547-
switch (categoryIndex.integerValue) {
548-
case 0: category = AVAudioSessionCategoryAmbient; break;
549-
case 1: category = AVAudioSessionCategorySoloAmbient; break;
550-
case 2: category = AVAudioSessionCategoryPlayback; break;
551-
#if AUDIO_SESSION_MICROPHONE
552-
case 3: category = AVAudioSessionCategoryRecord; break;
553-
case 4: category = AVAudioSessionCategoryPlayAndRecord; break;
554-
#endif
555-
case 5: category = AVAudioSessionCategoryMultiRoute; break;
541+
- (AVAudioSessionCategory)rawToCategory:(NSString *)rawCategory {
542+
for (AVAudioSessionCategory category in [[AVAudioSession sharedInstance] availableCategories]) {
543+
if ([category isEqualToString:rawCategory]) {
544+
return category;
556545
}
557546
}
558-
return category;
559-
}
560-
561-
- (NSObject *)categoryToFlutter:(AVAudioSessionCategory)category {
562-
if (category == AVAudioSessionCategoryAmbient) return @(0);
563-
else if (category == AVAudioSessionCategorySoloAmbient) return @(1);
564-
else if (category == AVAudioSessionCategoryPlayback) return @(2);
565-
#if AUDIO_SESSION_MICROPHONE
566-
else if (category == AVAudioSessionCategoryRecord) return @(3);
567-
else if (category == AVAudioSessionCategoryPlayAndRecord) return @(4);
568-
#endif
569-
else return @(5);
547+
return nil;
570548
}
571549

572550
- (NSString *)flutterToMode:(NSNumber *)modeIndex {

lib/src/core.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,8 @@ class AudioSessionConfiguration {
513513
: this(
514514
avAudioSessionCategory: data['avAudioSessionCategory'] == null
515515
? null
516-
: AVAudioSessionCategory
517-
.values[data['avAudioSessionCategory'] as int],
516+
: AVAudioSessionCategory.fromRawValue(
517+
data['avAudioSessionCategory'] as String),
518518
avAudioSessionCategoryOptions:
519519
data['avAudioSessionCategoryOptions'] == null
520520
? null
@@ -602,7 +602,7 @@ class AudioSessionConfiguration {
602602

603603
// Converts this instance to JSON.
604604
Map<String, dynamic> toJson() => {
605-
'avAudioSessionCategory': avAudioSessionCategory?.index,
605+
'avAudioSessionCategory': avAudioSessionCategory?.rawValue,
606606
'avAudioSessionCategoryOptions': avAudioSessionCategoryOptions?.value,
607607
'avAudioSessionMode': avAudioSessionMode?.index,
608608
'avAudioSessionRouteSharingPolicy':

lib/src/darwin.dart

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,8 @@ class AVAudioSession {
8787

8888
/// (UNTESTED)
8989
Future<AVAudioSessionCategory> get category async {
90-
final index = (await (_channel.invokeMethod<int>('getCategory')))!;
91-
return decodeEnum(AVAudioSessionCategory.values, index,
92-
defaultValue: AVAudioSessionCategory.playback);
90+
final rawCategory = (await (_channel.invokeMethod<String>('getCategory')))!;
91+
return AVAudioSessionCategory.fromRawValue(rawCategory);
9392
}
9493

9594
Future<void> setCategory(
@@ -99,14 +98,13 @@ class AVAudioSession {
9998
AVAudioSessionRouteSharingPolicy? policy,
10099
]) =>
101100
_channel.invokeMethod('setCategory',
102-
[category?.index, options?.value, mode?.index, policy?.index]);
101+
[category?.rawValue, options?.value, mode?.index, policy?.index]);
103102

104103
/// (UNTESTED)
105104
Future<List<AVAudioSessionCategory>> get availableCategories async =>
106105
(await _channel.invokeMethod<List<dynamic>>('getAvailableCategories'))!
107-
.cast<int>()
108-
.map((index) => decodeEnum(AVAudioSessionCategory.values, index,
109-
defaultValue: AVAudioSessionCategory.playback))
106+
.cast<String>()
107+
.map((rawValue) => AVAudioSessionCategory.fromRawValue(rawValue))
110108
.toList();
111109

112110
/// (UNTESTED)
@@ -345,12 +343,32 @@ class AVAudioSession {
345343

346344
/// The categories for [AVAudioSession].
347345
enum AVAudioSessionCategory {
348-
ambient,
349-
soloAmbient,
350-
playback,
351-
record,
352-
playAndRecord,
353-
multiRoute,
346+
ambient(_kAmbient),
347+
soloAmbient(_kSoloAmbient),
348+
playback(_kPlayback),
349+
record(_kRecord),
350+
playAndRecord(_kPlayAndRecord),
351+
multiRoute(_kMultiRoute);
352+
353+
static const _kAmbient = 'AVAudioSessionCategoryAmbient';
354+
static const _kSoloAmbient = 'AVAudioSessionCategorySoloAmbient';
355+
static const _kPlayback = 'AVAudioSessionCategoryPlayback';
356+
static const _kRecord = 'AVAudioSessionCategoryRecord';
357+
static const _kPlayAndRecord = 'AVAudioSessionCategoryPlayAndRecord';
358+
static const _kMultiRoute = 'AVAudioSessionCategoryMultiRoute';
359+
360+
static AVAudioSessionCategory fromRawValue(String rawValue) => const {
361+
_kAmbient: ambient,
362+
_kSoloAmbient: soloAmbient,
363+
_kPlayback: playback,
364+
_kRecord: record,
365+
_kPlayAndRecord: playAndRecord,
366+
_kMultiRoute: multiRoute,
367+
}[rawValue]!;
368+
369+
const AVAudioSessionCategory(this.rawValue);
370+
371+
final String rawValue;
354372
}
355373

356374
/// The category options for [AVAudioSession].

macos/Classes/AudioSessionPlugin.m

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
1717
- (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
1818
self = [super init];
1919
NSAssert(self, @"super init cannot be nil");
20-
_channel = [FlutterMethodChannel
21-
methodChannelWithName:@"com.ryanheise.audio_session"
20+
_channel = [FlutterMethodChannel
21+
methodChannelWithName:@"com.ryanheise.audio_session"
2222
binaryMessenger:[registrar messenger]];
23-
[registrar addMethodCallDelegate:self channel:_channel];
23+
[registrar addMethodCallDelegate:self channel:_channel];
2424
[plugins addObject:self];
2525
return self;
2626
}
@@ -31,17 +31,17 @@ - (FlutterMethodChannel *)channel {
3131

3232
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
3333
NSArray* args = (NSArray*)call.arguments;
34-
if ([@"setConfiguration" isEqualToString:call.method]) {
34+
if ([@"setConfiguration" isEqualToString:call.method]) {
3535
configuration = args[0];
3636
for (int i = 0; i < plugins.count; i++) {
3737
[plugins[i].channel invokeMethod:@"onConfigurationChanged" arguments:@[configuration]];
3838
}
39-
result(nil);
40-
} else if ([@"getConfiguration" isEqualToString:call.method]) {
39+
result(nil);
40+
} else if ([@"getConfiguration" isEqualToString:call.method]) {
4141
result(configuration);
42-
} else {
43-
result(FlutterMethodNotImplemented);
44-
}
42+
} else {
43+
result(FlutterMethodNotImplemented);
44+
}
4545
}
4646

4747
- (void) dealloc {

0 commit comments

Comments
 (0)