Maxim Irbe Maxim Irbe - 1 year ago 267
Objective-C Question

AVCaptureSession and background audio iOS 7

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];

'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
[audioSession setActive:YES error:nil];

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

Answer Source

You can use the AVAudioSessionCategoryOptionMiaxWithOthers. 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.