Update AudioSessionManager.mm

Signed-off-by: rohithzmoi <166651631+rohithzmoi@users.noreply.github.com>
This commit is contained in:
rohithzmoi 2024-09-03 14:49:58 +05:30 committed by GitHub
parent 72f3caa8e8
commit 73e197486c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 26 additions and 25 deletions

View File

@ -18,6 +18,8 @@
#import <AVFoundation/AVFoundation.h> #import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
static UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
extern "C" void setupAVAudioSession() { extern "C" void setupAVAudioSession() {
@try { @try {
@ -25,9 +27,7 @@ extern "C" void setupAVAudioSession() {
NSError *error = nil; NSError *error = nil;
NSLog(@"Setting up AVAudioSession..."); NSLog(@"Setting up AVAudioSession...");
[[NSNotificationCenter defaultCenter] removeObserver:session];
BOOL success = [session setCategory:AVAudioSessionCategoryPlayAndRecord BOOL success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionAllowBluetooth | withOptions:AVAudioSessionCategoryOptionAllowBluetooth |
AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionMixWithOthers |
@ -56,7 +56,7 @@ extern "C" void setupAVAudioSession() {
AVAudioSessionInterruptionType interruptionType = (AVAudioSessionInterruptionType)[note.userInfo[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue]; AVAudioSessionInterruptionType interruptionType = (AVAudioSessionInterruptionType)[note.userInfo[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
if (interruptionType == AVAudioSessionInterruptionTypeBegan) { if (interruptionType == AVAudioSessionInterruptionTypeBegan) {
NSLog(@"Audio session interruption began"); NSLog(@"Audio session interruption began");
// Pause audio processing
} else if (interruptionType == AVAudioSessionInterruptionTypeEnded) { } else if (interruptionType == AVAudioSessionInterruptionTypeEnded) {
NSLog(@"Audio session interruption ended, attempting to reactivate..."); NSLog(@"Audio session interruption ended, attempting to reactivate...");
NSError *activationError = nil; NSError *activationError = nil;
@ -65,7 +65,7 @@ extern "C" void setupAVAudioSession() {
NSLog(@"Error re-activating AVAudioSession after interruption: %@, code: %ld", activationError.localizedDescription, (long)activationError.code); NSLog(@"Error re-activating AVAudioSession after interruption: %@, code: %ld", activationError.localizedDescription, (long)activationError.code);
} else { } else {
NSLog(@"Audio session successfully reactivated after interruption"); NSLog(@"Audio session successfully reactivated after interruption");
// Resume audio processing
} }
} }
}]; }];
@ -112,24 +112,26 @@ extern "C" void deactivateAVAudioSession() {
} }
NSLog(@"AVAudioSession deactivated successfully"); NSLog(@"AVAudioSession deactivated successfully");
[[NSNotificationCenter defaultCenter] removeObserver:session];
} }
@catch (NSException *exception) { @catch (NSException *exception) {
NSLog(@"Exception deactivating AVAudioSession: %@", exception.reason); NSLog(@"Exception deactivating AVAudioSession: %@", exception.reason);
} }
} }
// Function to handle background audio setup
extern "C" void setupBackgroundAudio() { extern "C" void setupBackgroundAudio() {
@try { @try {
if (bgTask != UIBackgroundTaskInvalid) {
__block UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid; [[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"Background task expired. Cleaning up..."); NSLog(@"Background task expired. Cleaning up...");
[[UIApplication sharedApplication] endBackgroundTask:bgTask]; [[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid; bgTask = UIBackgroundTaskInvalid;
}]; }];
@ -144,7 +146,6 @@ extern "C" void setupBackgroundAudio() {
[session setActive:NO error:nil]; [session setActive:NO error:nil];
BOOL success = [session setCategory:AVAudioSessionCategoryPlayAndRecord BOOL success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker | withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker |
AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionAllowBluetooth |
@ -156,8 +157,6 @@ extern "C" void setupBackgroundAudio() {
NSLog(@"Error setting AVAudioSession category for background: %@, code: %ld", error.localizedDescription, (long)error.code); NSLog(@"Error setting AVAudioSession category for background: %@, code: %ld", error.localizedDescription, (long)error.code);
} else { } else {
NSLog(@"AVAudioSession category set successfully for background audio"); NSLog(@"AVAudioSession category set successfully for background audio");
success = [session setActive:YES error:&error]; success = [session setActive:YES error:&error];
if (!success || error) { if (!success || error) {
NSLog(@"Error activating AVAudioSession in background: %@, code: %ld", error.localizedDescription, (long)error.code); NSLog(@"Error activating AVAudioSession in background: %@, code: %ld", error.localizedDescription, (long)error.code);
@ -167,8 +166,10 @@ extern "C" void setupBackgroundAudio() {
} }
if (bgTask != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:bgTask]; [[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid; bgTask = UIBackgroundTaskInvalid;
}
}); });
} }
@catch (NSException *exception) { @catch (NSException *exception) {