AVCaptureSession and background audio iOS 7
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.
AVCaptureSession with only background music
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 {
}
AVCaptureSession, AVCaptureAudioDataOutput and background Music
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: [AVAudioSessionCategoryOptions.MixWithOthers, AVAudioSessionCategoryOptions.DefaultToSpeaker])
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print("error")
}
captureSession = AVCaptureSession()
iOS Camera: `AVCaptureAudioDataOutput` activate audio session on the fly, avoid background music stutter
Finally figured it out. I simply created a separate AVCaptureSession
specifically for the audio input/output which I synchronize with the main capture session's masterClock
. I can then start/stop the secondary capture session on the fly (shortly before start recording)
AVCaptureSession addInput causing glitch in background audio
Apparently there is no workaround.
https://forums.developer.apple.com/message/74778#74778
Background music cancelled when capturing audio in AVCaptureSession
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;
Related Topics
Looping Through Nsattributedstring Attributes to Increase Font Size
How to Call Presentviewcontroller from Within a Uicollectionviewcell
Swift 3: Type 'Any' Has No Subscript Members
Creating a Navigationcontroller Programmatically (Swift)
How to Turn the iPhone Camera Flash On/Off
Is a Date in Same Week, Month, Year of Another Date in Swift
Decode Base64Url to Base64 -- Swift
Firebase Cloud Messaging Doesn't Create Push Notifications But Gets Information
Open Uitableview Edit Action Buttons Programmatically
How to Get a Nsurl from an Xcassets Bundle
Firebase Cloud Messaging - Send Message to All Users
What Is a "Delegate" in Objective C's iPhone Development
How to Switch to Different Storyboard for iPhone 5
Single Function to Dismiss All Open View Controllers
Launch Image Not Showing Up in iOS Application (Using Images.Xcassets)
Recording to Aac from Remoteio: Data Is Getting Written But File Unplayable