Play Music While Camera Is Open

I am creating a camera app where I would like music from another app (Apple Music, Spotify, etc.) to continue playing once the app is opened. Currently I am using .mixWithOthers to do this in my viewDidLoad.

let audioSession = AVAudioSession.sharedInstance()
do {
      try audioSession.setCategory(AVAudioSession.Category.playback, options: [.mixWithOthers])
      try audioSession.setActive(true)
} catch {
      print("error trying to record and play audio")
}

However I am running into an issue where the music only plays if you resume music playback once you start recording a video. Otherwise, when you open the app music will stop when you see the preview. The interesting thing is that if you start playing music while recording, then once you stop music continues to play in the preview view. If you close the app (not force close) and reopen then music play back continues as expected. However, once you force close the app then it returns to the original behavior. I've tried to do research on this and I have not been able to find anything. Any help is appreciated. Let me know if more details are needed.

Answered by MGTech in 776485022

I was missing AVCaptureSession.automaticallyConfiguresApplicationAudioSession = false This was able to allow the .mixWithOthers to take effect

Accepted Answer

I was missing AVCaptureSession.automaticallyConfiguresApplicationAudioSession = false This was able to allow the .mixWithOthers to take effect

Play Music While Camera Is Open
 
 
Q