Skip to content
This repository was archived by the owner on Dec 1, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,27 @@ const setActive = (active: boolean): Promise<void> =>

const setCategory = (
category: AudioCategory,
options?: AudioCategoryOptions
): Promise<void> => RNAudioSession.setCategory(category, options)
options?: Array<AudioCategoryOptions> | AudioCategoryOptions
): Promise<void> => (
RNAudioSession.setCategory(
category,
Array.isArray(options) ? options : [options]
)
)

const setMode = (mode: AudioMode): Promise<void> => RNAudioSession.setMode(mode)

const setCategoryAndMode = (
category: AudioCategory,
mode: AudioMode,
options?: AudioCategoryOptions
): Promise<void> => RNAudioSession.setCategoryAndMode(category, mode, options)
options?: Array<AudioCategoryOptions> | AudioCategoryOptions
): Promise<void> => (
RNAudioSession.setCategoryAndMode(
category,
mode,
Array.isArray(options) ? options : [options]
)
)

const AudioSession = {
currentCategory,
Expand Down
28 changes: 21 additions & 7 deletions ios/RNAudioSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,21 @@ + (void)initialize {
}
}

RCT_EXPORT_METHOD(setCategory:(NSString *)category options:(NSString *)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
RCT_EXPORT_METHOD(setCategory:(NSString *)category options:(NSArray<NSString *> *)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
NSString* cat = _categories[category];
if (cat != nil && [[AVAudioSession sharedInstance].availableCategories containsObject:cat]) {
NSError *error = nil;
if (_options[options] != nil) {
[[AVAudioSession sharedInstance] setCategory:cat withOptions:[_options[options] integerValue] error:&error];
} else {
[[AVAudioSession sharedInstance] setCategory:cat error:&error];
AVAudioSessionCategoryOptions categoryOptions = 0;

for (NSString *option in options) {
if (_options[option] != nil) {
categoryOptions |= [_options[option] integerValue];
}
}

[[AVAudioSession sharedInstance] setCategory:cat withOptions:categoryOptions error:&error];

if (error) {
reject(@"setCategory", @"Could not set category.", error);
} else {
Expand Down Expand Up @@ -142,13 +147,22 @@ + (void)initialize {
}
}

RCT_EXPORT_METHOD(setCategoryAndMode:(NSString *)category mode:(NSString *)mode options:(NSString *)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
RCT_EXPORT_METHOD(setCategoryAndMode:(NSString *)category mode:(NSString *)mode options:(NSArray<NSString *> *)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
NSString* cat = _categories[category];
NSString* mod = _modes[mode];
if (cat != nil && mod != nil && _options[options] != nil && [[AVAudioSession sharedInstance].availableCategories containsObject:cat] && [[AVAudioSession sharedInstance].availableModes containsObject:mod]) {
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:cat mode:mod options:[_options[options] integerValue] error:&error];

AVAudioSessionCategoryOptions categoryOptions = 0;

for (NSString *option in options) {
if (_options[option] != nil) {
categoryOptions |= [_options[option] integerValue];
}
}

[[AVAudioSession sharedInstance] setCategory:cat mode:mod options:categoryOptions error:&error];
if (error) {
reject(@"setCategoryAndMode", @"Could not set category and mode.", error);
} else {
Expand Down