-->

How to resolve AVCaptureSession v AVAudioSession c

2019-04-12 12:21发布

问题:

I'm trying to run an AVCaptureSession in a view controller, but within the same, I'm also calling a function from a library that uses AVAudioSession. I can't seem to get much info out of the debugger, other than it crashes exactly when I call this particular library's function. The library is libpd:

https://github.com/libpd

and it calls AVAudioSession as sharedInstance. I call libpd as:

[self.audioController configurePlaybackWithSampleRate:44100 numberChannels:2 inputEnabled:YES mixingEnabled:YES]

so mixing is enabled, but just in case, I've recompiled it so that when it inits, I do:

    UInt32 doSetProperty = 1;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers,  sizeof(doSetProperty), &doSetProperty);

but, no luck. Moving the calls to libpd to viewWillAppear within the view controller didn't work either. However, if I take the code that calls libpd out of my viewcontroller, and put it in the app delegate within didFinishLaunchingWithOptions, it then starts just fine, and the two sessions seem to co-exist without crashing.

Am I missing something about AVCaptureSession and mixing? How do I go about both sessions co-existing? I'm not using AVCapture to capture audio, only camera input, so shouldn't I be able to somehow have both going on?

回答1:

Start the audio session (which can be set to support mixing) after you've started the camera session. I tried and you need to wait for the camera to be setup before you start the audio session (e.g. wait a couple of seconds)



回答2:

Is it possible that the library assumes there's no other audio session active and calls AudioSessionInitialize?

Per the docs "You may activate and deactivate your audio session as needed (see AudioSessionSetActive), but should initialize it only once."

I'd guess that the authors of this library didn't include functionality for an ongoing audio session... but it would be easy enough to dive in there and comment out the initializing lines, as long as your app always hits that function call with a running audio session (otherwise just check with an if statement to see if you have an audio session, if you don't, initialize one, etc.)