Fix high-definition bluetooth audio background playback

This commit is contained in:
Marc Rousavy 2021-03-26 15:22:27 +01:00
parent 0750e42e9f
commit 6bc31d922e

View File

@ -272,30 +272,32 @@ final class CameraView: UIView {
private let captureSession = AVCaptureSession() private let captureSession = AVCaptureSession()
private final func setAutomaticallyConfiguresAudioSession(_ automaticallyConfiguresAudioSession: Bool) {
if captureSession.automaticallyConfiguresApplicationAudioSession != automaticallyConfiguresAudioSession {
captureSession.beginConfiguration()
captureSession.automaticallyConfiguresApplicationAudioSession = automaticallyConfiguresAudioSession
captureSession.commitConfiguration()
}
}
// pragma MARK: Session, Device and Format Configuration // pragma MARK: Session, Device and Format Configuration
/** /**
Configures the Audio session to allow background-music playback while recording. Configures the Audio session to allow background-music playback while recording.
*/ */
private final func configureAudioSession() { private final func configureAudioSession() {
let start = DispatchTime.now() let start = DispatchTime.now()
let audioSession = AVAudioSession.sharedInstance()
do { do {
if captureSession.automaticallyConfiguresApplicationAudioSession { setAutomaticallyConfiguresAudioSession(false)
captureSession.beginConfiguration() let audioSession = AVAudioSession.sharedInstance()
captureSession.automaticallyConfiguresApplicationAudioSession = false
captureSession.commitConfiguration()
}
if audioSession.category != .playAndRecord { if audioSession.category != .playAndRecord {
// allow background music playback // allow background music playback
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playAndRecord, options: [.mixWithOthers, .allowBluetooth, .defaultToSpeaker]) try audioSession.setCategory(AVAudioSession.Category.playAndRecord, options: [.mixWithOthers, .allowBluetoothA2DP, .defaultToSpeaker])
} }
// activate current audio session because camera is active // activate current audio session because camera is active
try AVAudioSession.sharedInstance().setActive(true) try audioSession.setActive(true)
} catch let error as NSError { } catch let error as NSError {
self.invokeOnError(.session(.audioSessionSetupFailed(reason: error.localizedDescription)), cause: error) self.invokeOnError(.session(.audioSessionSetupFailed(reason: error.localizedDescription)), cause: error)
captureSession.beginConfiguration() setAutomaticallyConfiguresAudioSession(true)
captureSession.automaticallyConfiguresApplicationAudioSession = true
captureSession.commitConfiguration()
} }
let end = DispatchTime.now() let end = DispatchTime.now()
let nanoTime = end.uptimeNanoseconds - start.uptimeNanoseconds let nanoTime = end.uptimeNanoseconds - start.uptimeNanoseconds