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 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.
AVAudioSession configuration to record and play with others
I have almost met the similar problem and it is almost killed my one-two days for figure outing the problem.
In my case, I am used AVCaptureSession
for video/audio recording and it has property called automaticallyConfiguresApplicationAudioSession
. As per Apple docs
automaticallyConfiguresApplicationAudioSession: A Boolean value that indicates whether the capture session automatically changes
settings in the app’s shared audio session.The value of this property defaults to true, causing the capture
session to automatically configure the app’s shared AVAudioSession
instance for optimal recording
So If we are manually setting the AVAudioSession
, we should set this property as false
like below,
captureSession.automaticallyConfiguresApplicationAudioSession = false;
Making this property to false
and setting appropriate AVAudioSession
categories manually has solved my issue! Hope it will help in your case too!
Some more Observations on your code
- There is no need of
.notifyOthersOnDeactivation
flag to activating an audio
session This flag is used only when deactivating your audio session. That is when you pass a value offalse
in the beActive parameter of thesetActive(_:options:)
instance method. So the below code fine for active a sessiontry? AVAudioSession.sharedInstance().setActive(true)
In default the
AVAudioSessionCategoryPlayAndRecord
category leads
to a very low volume of background sound. If you want normal volume, need to use the option.defaultToSpeaker
with.mixWithOthers
as follows,try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.mixWithOthers, .defaultToSpeaker])
Oke! then I think It will now work fine if you have done all other stuff properly.
Note** I have created a working sample project for your reference and you can found it on GitHub Here
Related Topics
Periodically Call an API with Rxswift
Getting Uiwebview API Deprecation Message After Adding Paypal Pod in My iOS App
Swift 3 Imessage Extension Doesn't Open Url
How to Remove the Black Border of a Textfield
Core Data Taking Time to Insert Records with Fetching Entity & Set as Relationship
Uilocalnotification: Playing a Custom Audio File Saved in Documents Directory
Cocoapods, Add Dependencies to All Targets in a Generic Way
Reload View When @Published Is Changed
Xcode 6 Swift Wkwebview Keyboard Settings
How to Insert Items at 0 Index to the Realm Container
Open a Users Twitter Profile Page from iOS App
How to Send Different Users to Separate View Controllers Using Firebase and Xcode
How Is This Slide-Up Menu from the iPhone Messages App Implemented
Pausing Spritekit Game Using Appdelegate in iOS8