yun
yun

Reputation: 1283

Background Audio for iOS Video App

So I was working on a video capture app that plays background audio (from Spotify or Apple Music) and I'm having a small problem where there's a small audio interruption when I open my app while audio is being played.

Here's what I have for allowing background audio to play (located 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 stopping that beginning interruption? Thanks!!

EDIT


I should also mention after setting the AVAudioSession I am setting my AVCaptureSession. I initialize it then set the properties.

self.session.usesApplicationAudioSession = YES;
self.session.automaticallyConfiguresApplicationAudioSession = NO;

Upvotes: 8

Views: 289

Answers (1)

Okan Kurtuluş
Okan Kurtuluş

Reputation: 64

I think the reason of interruption is you are updating category in every case. You may use below function to check and update category only if it is needed.

-(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;
}

Upvotes: 2

Related Questions