From cd180dc73b6f60a29f138f6160b9b36addc72414 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Mon, 29 Mar 2021 13:18:02 +0200 Subject: [PATCH] Set `automaticallyConfiguresApplicationAudioSession = false` --- ios/CameraView+AVAudioSession.swift | 7 ++++++- ios/CameraView+AVCaptureSession.swift | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ios/CameraView+AVAudioSession.swift b/ios/CameraView+AVAudioSession.swift index 60b9834..5096867 100644 --- a/ios/CameraView+AVAudioSession.swift +++ b/ios/CameraView+AVAudioSession.swift @@ -20,13 +20,14 @@ extension CameraView { let start = DispatchTime.now() do { try addAudioInput() + let audioSession = AVAudioSession.sharedInstance() if audioSession.category != .playAndRecord { // allow background music playback try audioSession.setCategory(AVAudioSession.Category.playAndRecord, options: [.mixWithOthers, .allowBluetoothA2DP, .defaultToSpeaker]) } - // TODO: Use https://developer.apple.com/documentation/avfaudio/avaudiosession/3726094-setprefersnointerruptionsfromsys audioSession.trySetAllowHaptics(true) + // activate current audio session because camera is active try audioSession.setActive(true) } catch let error as NSError { @@ -38,6 +39,7 @@ extension CameraView { } self.removeAudioInput() } + let end = DispatchTime.now() let nanoTime = end.uptimeNanoseconds - start.uptimeNanoseconds ReactLogger.log(level: .info, message: "Configured Audio session in \(Double(nanoTime) / 1_000_000)ms!") @@ -52,6 +54,7 @@ extension CameraView { return } removeAudioInput() + captureSession.beginConfiguration() guard let audioDevice = AVCaptureDevice.default(for: .audio) else { throw CameraError.device(.microphoneUnavailable) @@ -72,6 +75,7 @@ extension CameraView { guard let audioInput = audioDeviceInput else { return } + captureSession.beginConfiguration() captureSession.removeInput(audioInput) audioDeviceInput = nil @@ -86,6 +90,7 @@ extension CameraView { let type = AVAudioSession.InterruptionType(rawValue: typeValue) else { return } + switch type { case .began: // Something interrupted our Audio Session, stop recording audio. diff --git a/ios/CameraView+AVCaptureSession.swift b/ios/CameraView+AVCaptureSession.swift index 6aaf8cb..2a01ee2 100644 --- a/ios/CameraView+AVCaptureSession.swift +++ b/ios/CameraView+AVCaptureSession.swift @@ -35,6 +35,10 @@ extension CameraView { captureSession.commitConfiguration() } + // Disable automatic Audio Session configuration because we configure it in CameraView+AVAudioSession.swift (called before Camera gets activated) + captureSession.automaticallyConfiguresApplicationAudioSession = false + + // If preset is set, use preset. Otherwise use format. if let preset = self.preset { var sessionPreset: AVCaptureSession.Preset? do {