15

Whenever I start an AVCaptureSession running with the microphone as an input it cancels whatever background music is currently running (iPod music for instance). If I comment out the line adding the audio input, the background audio continues.

Does anyone know a way to record video clips with the microphone while continuing to allow background audio to play? Also there is error, when you trying to record video and the music is currently playing.

A tried to do like this:

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];

But 'AudioSessionSetProperty' is deprecated: first deprecated in iOS 7.0

So I tried to do like this:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil; 
[audioSession setCategory:AVAudioSessionCategoryPlayback
              withOptions:AVAudioSessionCategoryOptionMixWithOthers
                    error:&setCategoryError];
[audioSession setActive:YES error:nil];

But finally it didn't work. Thanks for help!

Maxim Irbe
  • 203
  • 4
  • 7

2 Answers2

31

In your AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Stop app pausing other sound.
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
                                     withOptions:AVAudioSessionCategoryOptionDuckOthers | AVAudioSessionCategoryOptionDefaultToSpeaker
                                           error:nil];
}

Where you are allocating AVCaptureSession:

AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.automaticallyConfiguresApplicationAudioSession = NO;

This code will allow you to play background music and run AVCaptureSession with the microphone.

SWIFT UPDATE:

AppDelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    //Stop app pausing other sound.
    do{
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, 
                                            withOptions: [.DuckOthers, .DefaultToSpeaker])
    }
    catch {

    }

    return true
}

Where you are allocating AVCaptureSession:

let session = AVCaptureSession()
session.automaticallyConfiguresApplicationAudioSession = false
Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
  • 3
    It worked.. I spent almost 3days for this.. Thanx a ton Gabriel. – Shreya Jul 09 '15 at 10:53
  • 1
    Gabriel, I started a bounty with this exact question and after many days of searching for the answer I found it with your answer. I would gladly give you the bounty if you want. – Lehman2020 Aug 16 '15 at 20:53
  • http://stackoverflow.com/questions/31860593/how-to-record-video-and-maintain-music-playing-in-the-background – Lehman2020 Aug 16 '15 at 20:53
  • 1
    @Gabriel.Massana - this works well but there is clear initial dimming of the background music..do you have any idea how to bypass this? Thanks! – trdavidson Mar 14 '16 at 04:23
  • @trdavidson Sorry, no solution for the sound glitch. – Gabriel.Massana Mar 14 '16 at 07:03
  • 1
    @Gabriel.Massana that's a shame man - thanks for the quick response! I'll report back if I end up solving it. – trdavidson Mar 14 '16 at 16:36
  • I get an AVCaptureSession runtime error when trying these. Has anyone had that before? – Faris Sbahi Dec 24 '16 at 09:56
  • @trdavidson hi. I know this is very late but have you fix the initial dimming issue? This link is the most sensible I got so far in Google search result. I'm about to try the other option MixWithOthers though I'm not really sure how it works – mr5 Jun 12 '18 at 06:56
  • Just doing session.automaticallyConfiguresApplicationAudioSession = false resolved the issue in my case. Good work! – cora Feb 05 '21 at 15:58
19

You can use the AVAudioSessionCategoryOptionMixWithOthers. For instance,

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];

After that, you can use the AVAudioPlayer simultaneously with AVCaptureSession.

However, the above code leads to very low volume. If you want normal volume, use the AVAudioSessionCategoryOptionDefaultToSpeaker with AVAudioSessionCategoryOptionMixWithOthers as follows,

[session setCategory:AVAudioSessionCategoryPlayAndRecord  withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];

This goes well.

donkim
  • 13,119
  • 3
  • 42
  • 47
KNaito
  • 3,930
  • 2
  • 19
  • 21
  • 2
    This doesn't go well for me. When I call the last line, the background audio stops playing. – AndroC Jun 03 '15 at 08:47
  • What is your iOS version? I have not yet checked iOS8. Apple may change the specification. – KNaito Jun 04 '15 at 00:42
  • 3
    I discovered that it does work when its put in `AppDelegate didFinishLaunching`. However, in this case there is a noticeable pause in the background audio while the app is starting. If I want to start the app in `Ambient` and then later switch to `PlayAndRecord`, first I have to call `setActive:NO withOptions:NotifyOnDeactivation` and after setting the `PlayAndRecord` category I have to re-activate `setActive:YES`. All this switching causes brief pausing of the background audio and visual glitches in my `AVCaptureSession`. – AndroC Jun 04 '15 at 08:11
  • 1
    Thanks ancajic for valuable report. – KNaito Jun 06 '15 at 00:57
  • I have added the above two lines of code in my view controller viewDidLoad. After recording (through headset) my music file output will be a combination of the sound recorded through mic + the background music played through the speaker. But my issue is that the sound of the background music played through the speaker is very low on the final output music. – Abhijith G Aug 13 '15 at 10:22
  • ancajic, have you got any solution to avoid this glitches ? I am using `PlayAndRecord` with `setActive:YES` and faced same glitch. If you found any solution, please let me know. – Surjeet Singh Oct 08 '15 at 06:52
  • @ancajic how did you successfully switch the category? I can't get it to work: http://stackoverflow.com/questions/35615707/avaudiosession-setcategory-not-working – Cbas Feb 25 '16 at 00:19