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 |
@ -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,7 +65,7 @@ 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
}
}
}];
@ -112,24 +112,26 @@ extern "C" void deactivateAVAudioSession() {
}
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;
}];
@ -144,7 +146,6 @@ extern "C" void setupBackgroundAudio() {
[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);
@ -167,8 +166,10 @@ extern "C" void setupBackgroundAudio() {
}
if (bgTask != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
}
@catch (NSException *exception) {