Avcapturesession and Avaudiosession Recording Video While Background Music Playing Only Works Once

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 of false in the beActive parameter of the setActive(_:options:) instance method. So the below code fine for active a session
    try? 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



Leave a reply



Submit