doorman
doorman

Reputation: 16929

Background music cancelled when capturing audio in AVCaptureSession

I would like background music to continue playing when I am inside my iOS app. However when ever I enter my app the background music is cancelled. I isolated the problem to the audio part of the AVCaptureSession:

var audioDevice = AVCaptureDevice.DefaultDeviceWithMediaType(AVMediaType.Audio);
var audioInput = AVCaptureDeviceInput.FromDevice(audioDevice);
captureSession.AddInput(audioInput);

I tried setting the AudioSession immediately after in two ways:

AudioSession.Initialize();
AudioSession.Category = AudioSessionCategory.AmbientSound;
AudioSession.SetActive(true);

or like this:

AudioSession.Initialize();
AudioSession.Category = AudioSessionCategory.MediaPlayback;
AudioSession.OverrideCategoryMixWithOthers = true;
AudioSession.SetActive(true);

But neither worked.

Upvotes: 0

Views: 307

Answers (1)

Geir Smestad
Geir Smestad

Reputation: 1384

Have you tried explicitly creating an instance of the session object and setting MixWithOthers, as described here? I'm thinking the Xamarin equivalent would be something like

AVAudioSession session = AVAudioSession.SharedInstance();
session.SetCategory(AVAudioSessionCategory.PlayAndRecord, AVAudioSessionCategoryOptions.MixWithOthers | AVAudioSessionCategoryOptions.DefaultToSpeaker);

Alternatively, as suggested in the link above, try setting the following in your AppDelegate:

AudioSession.Category = AudioSessionCategory.PlayAndRecord;
AudioSession.AudioShouldDuck = true;
AudioSession.OverrideCategoryDefaultToSpeaker = true;

...and then creating your AVCaptureSession like this:

AVCaptureSession session2 = new AVCaptureSession();
session2.AutomaticallyConfiguresApplicationAudioSession = false;

Upvotes: 1

Related Questions