From c4c86e143460ad28dcacfaabc5ffcacaee015f47 Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Sun, 7 Jan 2024 17:52:28 +0100 Subject: [PATCH] Fix audio session --- IceCubesApp/App/Main/IceCubesApp.swift | 3 ++- .../MediaUI/MediaUIAttachmentVideoView.swift | 18 ++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/IceCubesApp/App/Main/IceCubesApp.swift b/IceCubesApp/App/Main/IceCubesApp.swift index 2df414b5..83f009ad 100644 --- a/IceCubesApp/App/Main/IceCubesApp.swift +++ b/IceCubesApp/App/Main/IceCubesApp.swift @@ -83,7 +83,8 @@ class AppDelegate: NSObject, UIApplicationDelegate { func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { - try? AVAudioSession.sharedInstance().setCategory(.ambient) + try? AVAudioSession.sharedInstance().setCategory(.ambient, options: .mixWithOthers) + try? AVAudioSession.sharedInstance().setActive(true) PushNotificationsService.shared.setAccounts(accounts: AppAccountsManager.shared.pushAccounts) return true } diff --git a/Packages/MediaUI/Sources/MediaUI/MediaUIAttachmentVideoView.swift b/Packages/MediaUI/Sources/MediaUI/MediaUIAttachmentVideoView.swift index bfe2c19a..ed40c4f2 100644 --- a/Packages/MediaUI/Sources/MediaUI/MediaUIAttachmentVideoView.swift +++ b/Packages/MediaUI/Sources/MediaUI/MediaUIAttachmentVideoView.swift @@ -78,19 +78,9 @@ public struct MediaUIAttachmentVideoView: View { viewModel.preparePlayer(autoPlay: isFullScreen ? true : preferences.autoPlayVideo, isCompact: isCompact) viewModel.mute(preferences.muteVideo) - - DispatchQueue.global().async { - try? AVAudioSession.sharedInstance().setCategory(.playback, options: .duckOthers) - try? AVAudioSession.sharedInstance().setActive(true) - } } .onDisappear { viewModel.pause() - - DispatchQueue.global().async { - try? AVAudioSession.sharedInstance().setCategory(.ambient, options: .mixWithOthers) - try? AVAudioSession.sharedInstance().setActive(true) - } } .onTapGesture { if !preferences.autoPlayVideo && !viewModel.isPlaying { @@ -111,6 +101,11 @@ public struct MediaUIAttachmentVideoView: View { } } .onAppear { + DispatchQueue.global().async { + try? AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation) + try? AVAudioSession.sharedInstance().setCategory(.playback, options: .duckOthers) + try? AVAudioSession.sharedInstance().setActive(true) + } if isCompact || !preferences.autoPlayVideo { viewModel.play() } @@ -121,6 +116,9 @@ public struct MediaUIAttachmentVideoView: View { viewModel.pause() } viewModel.mute(preferences.muteVideo) + try? AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation) + try? AVAudioSession.sharedInstance().setCategory(.ambient, options: .mixWithOthers) + try? AVAudioSession.sharedInstance().setActive(true) } } .cornerRadius(4)