Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 524
yun Avatar asked Jun 22 '16 22:06

yun


People also ask

How do you add background music to an iPhone video?

With your project open, tap the Add Media button , then tap Audio. To find music previously synced to your device from your computer, tap My Music. To listen to the music before adding it, tap the item. To add the music to your movie project, tap the item, then tap the Add Audio button .

Which app is best for adding background music to video?

Download the Best App to Add Music to Videos Start by downloading the free YouCam Video app on your app store or Google Play. With a high 4.7 app rating and over 3,000 reviews, it is the best app to add music to videos for iPhone and Android in 2022.

How do I add custom background sounds to my iPhone?

Go to Settings > Accessibility > Audio/Visual > Background Sounds, then turn on Background Sounds. Set any of the following: Sound: Choose a sound; the audio file downloads to your iPhone.


1 Answers

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;
}
like image 90
Okan Kurtuluş Avatar answered Oct 18 '22 05:10

Okan Kurtuluş