Skip to content
Merged
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
11 changes: 10 additions & 1 deletion packages/audiodocs/docs/system/audio-manager.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,16 @@ Resets all of the lock screen data.
| :---: | :---: | :---- |
| enabled | `boolean` | It is used to set/unset [AVAudioSession](https://developer.apple.com/documentation/avfaudio/avaudiosession?language=objc#Activating-the-audio-configuration) activity |

#### Returns promise of `boolean` type, which is resolved to `true` if invokation ended with success, `false` otherwise.
#### Returns promise of `boolean` type, which is resolved to `true` if invokation ended with success, `false` otherwise.'

### `disableSessionManagement` <OnlyiOS />

#### Returns `undefined`.

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.
Typical use-case for this method is when user wants to fully control audio session outside of `react-native-audio-api` package,
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.
Any later call to `setAudioSessionOptions` or `setAudioSessionActivity` will re-enable internal audio session management.

### `getDevicePreferredSampleRate`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ class AudioAPIModule(
// noting to do here
}

override fun disableSessionManagement() {
// nothing to do here
}

override fun setLockScreenInfo(info: ReadableMap?) {
MediaSessionManager.setLockScreenInfo(info)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public NativeAudioAPIModuleSpec(ReactApplicationContext reactContext) {
@DoNotStrip
public abstract void setAudioSessionOptions(String category, String mode, ReadableArray options, boolean allowHaptics);

@ReactMethod
@DoNotStrip
public abstract void disableSessionManagement();

@ReactMethod
@DoNotStrip
public abstract void setLockScreenInfo(ReadableMap info);
Expand Down
11 changes: 11 additions & 0 deletions packages/react-native-audio-api/ios/audioapi/ios/AudioAPIModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ - (void)invalidate
setAudioSessionActivity : (BOOL)enabled resolve : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)
reject)
{
if (!self.audioSessionManager.shouldManageSession) {
[self.audioSessionManager setShouldManageSession:true];
}
if ([self.audioSessionManager setActive:enabled]) {
resolve(@"true");
return;
Expand All @@ -130,6 +133,9 @@ - (void)invalidate
setAudioSessionOptions : (NSString *)category mode : (NSString *)mode options : (NSArray *)
options allowHaptics : (BOOL)allowHaptics)
{
if (!self.audioSessionManager.shouldManageSession) {
[self.audioSessionManager setShouldManageSession:true];
}
[self.audioSessionManager setAudioSessionOptions:category mode:mode options:options allowHaptics:allowHaptics];
}

Expand Down Expand Up @@ -182,6 +188,11 @@ - (void)invalidate
[self.audioSessionManager getDevicesInfo:resolve reject:reject];
}

RCT_EXPORT_METHOD(disableSessionManagement)
{
[self.audioSessionManager disableSessionManagement];
}

#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@property (nonatomic, assign) AVAudioSessionCategory sessionCategory;
@property (nonatomic, assign) AVAudioSessionCategoryOptions sessionOptions;
@property (nonatomic, assign) bool allowHapticsAndSystemSoundsDuringRecording;
@property (nonatomic, assign) bool shouldManageSession;

- (instancetype)init;
- (void)cleanup;
Expand All @@ -27,6 +28,7 @@
options:(NSArray *)options
allowHaptics:(BOOL)allowHaptics;
- (bool)setActive:(bool)active;
- (void)disableSessionManagement;

- (void)requestRecordingPermissions:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject;
- (void)checkRecordingPermissions:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ - (instancetype)init
self.allowHapticsAndSystemSoundsDuringRecording = false;
self.hasDirtySettings = true;
self.isActive = false;
self.shouldManageSession = true;
}

return self;
Expand Down Expand Up @@ -135,6 +136,9 @@ - (void)setAudioSessionOptions:(NSString *)category

- (bool)setActive:(bool)active
{
if (!self.shouldManageSession) {
return true;
}
if (active == self.isActive) {
return true;
}
Expand Down Expand Up @@ -162,6 +166,11 @@ - (bool)setActive:(bool)active

- (bool)configureAudioSession
{
if (!self.shouldManageSession) {
NSLog(@"[AudioSessionManager] Skipping AVAudioSession configuration, shouldManageSession is false");
return true;
}

if (![self hasDirtySettings]) {
return true;
}
Expand Down Expand Up @@ -211,6 +220,12 @@ - (void)markSettingsAsDirty
self.isActive = false;
}

- (void)disableSessionManagement
{
self.shouldManageSession = false;
self.hasDirtySettings = false;
}

- (void)requestRecordingPermissions:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject
{
id value = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSMicrophoneUsageDescription"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface Spec extends TurboModule {
options: Array<string>,
allowHaptics: boolean
): void;
disableSessionManagement(): void;

// Lock Screen Info
setLockScreenInfo(info: {
Expand Down
4 changes: 4 additions & 0 deletions packages/react-native-audio-api/src/system/AudioManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class AudioManager {
);
}

disableSessionManagement() {
NativeAudioAPIModule!.disableSessionManagement();
}

setLockScreenInfo(info: LockScreenInfo) {
NativeAudioAPIModule!.setLockScreenInfo(info);
}
Expand Down