Background sound for iOS video application

So, I was working on a video capture application that plays background audio (from Spotify or Apple Music), and I have a small problem when there is a slight interruption in sound when opening my application during audio playback.

Here I have the ability to allow background sound to play (found in my didFinishLaunchingWithOptions in my AppDelegate class:

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionMixWithOthers error:nil]; [[AVAudioSession sharedInstance] setActive:YES error:nil]; 

Any clues to stop this beginning of the interrupt? Thanks!!

EDIT


I should also mention after installing AVAudioSession. I install my AVCaptureSession. I initialize it, then set the properties.

 self.session.usesApplicationAudioSession = YES; self.session.automaticallyConfiguresApplicationAudioSession = NO; 
+8
ios objective-c audio avcapturesession
source share
1 answer

I think the reason for the interruption is that you are updating the category in each case. You can use the function below to check and update a category only if necessary.

 -(BOOL) checkAndUpdateCategory { NSError *error; AVAudioSession *session = [AVAudioSession sharedInstance]; BOOL result = [session.category isEqualToString:AVAudioSessionCategoryPlayAndRecord]; if(!result) { result = [session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionMixWithOthers error:&error]; if(error) { //Handle Error NSLOG(@"Error:%@", error); } } return result; } 
+2
source share

All Articles