I need to do something like this. That my app do recording using AVCapturesession but it should be able to stream the live feed with background music that I had played.
Remember i am able to play background music with the AVCapturesession,but the problem is that it also involve the sound of surrounding environment.
Here is the audiosession category I am using
AudioSessionInitialize(NULL, NULL, NULL, self);
// //set the audio category
UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &audioCategory);
// mix with others!! this allows using AVCaptureSession and AusioSession simultaniously
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
AudioSessionSetActive(YES);
I have tried out almost all combination of category but I am not able to get required output, SoloAmbient and Ambient don't play sound at all.
I had also tried to disable audioinput of the AVCapturesession but it does not help.
You'll have to remove the AVCaptureInput device that represents the microphone. You can do this by iterating over the AVCaptureSession instance and doing something like this:
AVCaptureSession *currentSession = self.currentSession;
for(AVCaptureInput *input in currentSession.inputs) {
for (AVCaptureInputPort *port in input.ports) {
if ([[port mediaType] isEqual:AVMediaTypeAudio]) {
[currentSession removeInput:input];
break;
}
}
}
now setup your audio as something like this:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
NSError *activationError = nil;
BOOL success = [session setActive: YES error: &activationError];
SWIFT 3
let currentSession = self.currentSession
for input in currentSession.inputs as! [AVCaptureInput] {
for port in input.ports as! [AVCaptureInputPort] {
if port.mediaType == AVMediaTypeAudio {
currentSession.removeInput(input)
break
}
}
}
do {
let session = AVAudioSession.sharedInstance()
try session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [AVAudioSessionCategoryOptions.mixWithOthers, AVAudioSessionCategoryOptions.defaultToSpeaker])
try session.setActive(true)
} catch {
}