Skip to content

Commit cc082c7

Browse files
feat: option to disable avaudiosession configuration / management (#770)
* feat: native impl of disableSessionManagement * feat: ts api for disableSessionManagement * feat: docs * feat: add empty method for android
1 parent 160484c commit cc082c7

File tree

8 files changed

+51
-1
lines changed

8 files changed

+51
-1
lines changed

packages/audiodocs/docs/system/audio-manager.mdx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,16 @@ Resets all of the lock screen data.
102102
| :---: | :---: | :---- |
103103
| enabled | `boolean` | It is used to set/unset [AVAudioSession](https://developer.apple.com/documentation/avfaudio/avaudiosession?language=objc#Activating-the-audio-configuration) activity |
104104

105-
#### Returns promise of `boolean` type, which is resolved to `true` if invokation ended with success, `false` otherwise.
105+
#### Returns promise of `boolean` type, which is resolved to `true` if invokation ended with success, `false` otherwise.'
106+
107+
### `disableSessionManagement` <OnlyiOS />
108+
109+
#### Returns `undefined`.
110+
111+
Disables all internal default [AVAudioSession](https://developer.apple.com/documentation/avfaudio/avaudiosession) configurations and management done by the `react-native-audio-api` package. After calling this method, user is responsible for managing audio session entirely on their own.
112+
Typical use-case for this method is when user wants to fully control audio session outside of `react-native-audio-api` package,
113+
commonly when using another audio library along `react-native-audio-api`. The method has to be called before `AudioContext` is created, for example in app initialization code.
114+
Any later call to `setAudioSessionOptions` or `setAudioSessionActivity` will re-enable internal audio session management.
106115

107116
### `getDevicePreferredSampleRate`
108117

packages/react-native-audio-api/android/src/main/java/com/swmansion/audioapi/AudioAPIModule.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ class AudioAPIModule(
110110
// noting to do here
111111
}
112112

113+
override fun disableSessionManagement() {
114+
// nothing to do here
115+
}
116+
113117
override fun setLockScreenInfo(info: ReadableMap?) {
114118
MediaSessionManager.setLockScreenInfo(info)
115119
}

packages/react-native-audio-api/android/src/oldarch/NativeAudioAPIModuleSpec.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public NativeAudioAPIModuleSpec(ReactApplicationContext reactContext) {
5050
@DoNotStrip
5151
public abstract void setAudioSessionOptions(String category, String mode, ReadableArray options, boolean allowHaptics);
5252

53+
@ReactMethod
54+
@DoNotStrip
55+
public abstract void disableSessionManagement();
56+
5357
@ReactMethod
5458
@DoNotStrip
5559
public abstract void setLockScreenInfo(ReadableMap info);

packages/react-native-audio-api/ios/audioapi/ios/AudioAPIModule.mm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ - (void)invalidate
118118
setAudioSessionActivity : (BOOL)enabled resolve : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)
119119
reject)
120120
{
121+
if (!self.audioSessionManager.shouldManageSession) {
122+
[self.audioSessionManager setShouldManageSession:true];
123+
}
121124
if ([self.audioSessionManager setActive:enabled]) {
122125
resolve(@"true");
123126
return;
@@ -130,6 +133,9 @@ - (void)invalidate
130133
setAudioSessionOptions : (NSString *)category mode : (NSString *)mode options : (NSArray *)
131134
options allowHaptics : (BOOL)allowHaptics)
132135
{
136+
if (!self.audioSessionManager.shouldManageSession) {
137+
[self.audioSessionManager setShouldManageSession:true];
138+
}
133139
[self.audioSessionManager setAudioSessionOptions:category mode:mode options:options allowHaptics:allowHaptics];
134140
}
135141

@@ -182,6 +188,11 @@ - (void)invalidate
182188
[self.audioSessionManager getDevicesInfo:resolve reject:reject];
183189
}
184190

191+
RCT_EXPORT_METHOD(disableSessionManagement)
192+
{
193+
[self.audioSessionManager disableSessionManagement];
194+
}
195+
185196
#ifdef RCT_NEW_ARCH_ENABLED
186197
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
187198
(const facebook::react::ObjCTurboModule::InitParams &)params

packages/react-native-audio-api/ios/audioapi/ios/system/AudioSessionManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
@property (nonatomic, assign) AVAudioSessionCategory sessionCategory;
1515
@property (nonatomic, assign) AVAudioSessionCategoryOptions sessionOptions;
1616
@property (nonatomic, assign) bool allowHapticsAndSystemSoundsDuringRecording;
17+
@property (nonatomic, assign) bool shouldManageSession;
1718

1819
- (instancetype)init;
1920
- (void)cleanup;
@@ -27,6 +28,7 @@
2728
options:(NSArray *)options
2829
allowHaptics:(BOOL)allowHaptics;
2930
- (bool)setActive:(bool)active;
31+
- (void)disableSessionManagement;
3032

3133
- (void)requestRecordingPermissions:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject;
3234
- (void)checkRecordingPermissions:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject;

packages/react-native-audio-api/ios/audioapi/ios/system/AudioSessionManager.mm

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ - (instancetype)init
1313
self.allowHapticsAndSystemSoundsDuringRecording = false;
1414
self.hasDirtySettings = true;
1515
self.isActive = false;
16+
self.shouldManageSession = true;
1617
}
1718

1819
return self;
@@ -135,6 +136,9 @@ - (void)setAudioSessionOptions:(NSString *)category
135136

136137
- (bool)setActive:(bool)active
137138
{
139+
if (!self.shouldManageSession) {
140+
return true;
141+
}
138142
if (active == self.isActive) {
139143
return true;
140144
}
@@ -162,6 +166,11 @@ - (bool)setActive:(bool)active
162166

163167
- (bool)configureAudioSession
164168
{
169+
if (!self.shouldManageSession) {
170+
NSLog(@"[AudioSessionManager] Skipping AVAudioSession configuration, shouldManageSession is false");
171+
return true;
172+
}
173+
165174
if (![self hasDirtySettings]) {
166175
return true;
167176
}
@@ -211,6 +220,12 @@ - (void)markSettingsAsDirty
211220
self.isActive = false;
212221
}
213222

223+
- (void)disableSessionManagement
224+
{
225+
self.shouldManageSession = false;
226+
self.hasDirtySettings = false;
227+
}
228+
214229
- (void)requestRecordingPermissions:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject
215230
{
216231
id value = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSMicrophoneUsageDescription"];

packages/react-native-audio-api/src/specs/NativeAudioAPIModule.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface Spec extends TurboModule {
1515
options: Array<string>,
1616
allowHaptics: boolean
1717
): void;
18+
disableSessionManagement(): void;
1819

1920
// Lock Screen Info
2021
setLockScreenInfo(info: {

packages/react-native-audio-api/src/system/AudioManager.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ class AudioManager {
4545
);
4646
}
4747

48+
disableSessionManagement() {
49+
NativeAudioAPIModule!.disableSessionManagement();
50+
}
51+
4852
setLockScreenInfo(info: LockScreenInfo) {
4953
NativeAudioAPIModule!.setLockScreenInfo(info);
5054
}

0 commit comments

Comments
 (0)