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 <UIKit/UIKit.h>
static UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
extern "C" void setupAVAudioSession() {
@try {
@ -25,9 +27,7 @@ extern "C" void setupAVAudioSession() {
NSError *error = nil;
NSLog(@"Setting up AVAudioSession...");
[[NSNotificationCenter defaultCenter] removeObserver:session];
BOOL success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionAllowBluetooth |
AVAudioSessionCategoryOptionMixWithOthers |
@ -36,11 +36,11 @@ extern "C" void setupAVAudioSession() {
if (!success || error) {
NSLog(@"Error setting AVAudioSession category: %@, code: %ld", error.localizedDescription, (long)error.code);
return;
return;
}
NSLog(@"AVAudioSession category set to PlayAndRecord with DefaultToSpeaker option");
success = [session setActive:YES error:&error];
if (!success || error) {
NSLog(@"Error activating AVAudioSession: %@, code: %ld", error.localizedDescription, (long)error.code);
@ -56,7 +56,7 @@ extern "C" void setupAVAudioSession() {
AVAudioSessionInterruptionType interruptionType = (AVAudioSessionInterruptionType)[note.userInfo[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
if (interruptionType == AVAudioSessionInterruptionTypeBegan) {
NSLog(@"Audio session interruption began");
// Pause audio processing
} else if (interruptionType == AVAudioSessionInterruptionTypeEnded) {
NSLog(@"Audio session interruption ended, attempting to reactivate...");
NSError *activationError = nil;
@ -65,12 +65,12 @@ extern "C" void setupAVAudioSession() {
NSLog(@"Error re-activating AVAudioSession after interruption: %@, code: %ld", activationError.localizedDescription, (long)activationError.code);
} else {
NSLog(@"Audio session successfully reactivated after interruption");
// Resume audio processing
}
}
}];
[[NSNotificationCenter defaultCenter] addObserverForName:AVAudioSessionRouteChangeNotification
object:session
queue:[NSOperationQueue mainQueue]
@ -104,47 +104,48 @@ extern "C" void deactivateAVAudioSession() {
NSLog(@"Deactivating AVAudioSession...");
BOOL success = [session setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
if (!success || error) {
NSLog(@"Error deactivating AVAudioSession: %@, code: %ld", error.localizedDescription, (long)error.code);
return;
return;
}
NSLog(@"AVAudioSession deactivated successfully");
[[NSNotificationCenter defaultCenter] removeObserver:session];
}
@catch (NSException *exception) {
NSLog(@"Exception deactivating AVAudioSession: %@", exception.reason);
}
}
// Function to handle background audio setup
extern "C" void setupBackgroundAudio() {
@try {
__block UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
if (bgTask != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"Background task expired. Cleaning up...");
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error = nil;
NSLog(@"Configuring AVAudioSession for background...");
[session setActive:NO error:nil];
BOOL success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker |
AVAudioSessionCategoryOptionAllowBluetooth |
@ -156,8 +157,6 @@ extern "C" void setupBackgroundAudio() {
NSLog(@"Error setting AVAudioSession category for background: %@, code: %ld", error.localizedDescription, (long)error.code);
} else {
NSLog(@"AVAudioSession category set successfully for background audio");
success = [session setActive:YES error:&error];
if (!success || error) {
NSLog(@"Error activating AVAudioSession in background: %@, code: %ld", error.localizedDescription, (long)error.code);
@ -166,9 +165,11 @@ extern "C" void setupBackgroundAudio() {
}
}
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
if (bgTask != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
}
@catch (NSException *exception) {