From 9e429463b267a7fa58ea54aa8933378e2d1128d6 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 4 Apr 2024 17:31:16 -0400 Subject: [PATCH] Make the audio session work better Closes #353 Closes #443 --- Tusker.xcodeproj/project.pbxproj | 4 --- Tusker/AppDelegate.swift | 5 ---- Tusker/AudioSessionHelper.swift | 28 ------------------- .../VideoGalleryContentViewController.swift | 19 +++++++++++-- 4 files changed, 16 insertions(+), 40 deletions(-) delete mode 100644 Tusker/AudioSessionHelper.swift diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index eb069454..ec171d99 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -282,7 +282,6 @@ D6C7D27D22B6EBF800071952 /* AttachmentsContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6C7D27C22B6EBF800071952 /* AttachmentsContainerView.swift */; }; D6C82B4125C5BB7E0017F1E6 /* ExploreViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6C82B4025C5BB7E0017F1E6 /* ExploreViewController.swift */; }; D6C94D892139E6EC00CB5196 /* AttachmentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6C94D882139E6EC00CB5196 /* AttachmentView.swift */; }; - D6CA6A92249FAD8900AD45C1 /* AudioSessionHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6CA6A91249FAD8900AD45C1 /* AudioSessionHelper.swift */; }; D6CA6ED229EF6091003EC5DF /* TuskerPreferences in Frameworks */ = {isa = PBXBuildFile; productRef = D6CA6ED129EF6091003EC5DF /* TuskerPreferences */; }; D6CA8CDA2962231F0050C433 /* ArrayUniqueTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6CA8CD92962231F0050C433 /* ArrayUniqueTests.swift */; }; D6CA8CDE296387310050C433 /* SaveToPhotosActivity.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6CA8CDD296387310050C433 /* SaveToPhotosActivity.swift */; }; @@ -685,7 +684,6 @@ D6C7D27C22B6EBF800071952 /* AttachmentsContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttachmentsContainerView.swift; sourceTree = ""; }; D6C82B4025C5BB7E0017F1E6 /* ExploreViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExploreViewController.swift; sourceTree = ""; }; D6C94D882139E6EC00CB5196 /* AttachmentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttachmentView.swift; sourceTree = ""; }; - D6CA6A91249FAD8900AD45C1 /* AudioSessionHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioSessionHelper.swift; sourceTree = ""; }; D6CA6ED029EF6060003EC5DF /* TuskerPreferences */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = TuskerPreferences; sourceTree = ""; }; D6CA8CD92962231F0050C433 /* ArrayUniqueTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrayUniqueTests.swift; sourceTree = ""; }; D6CA8CDD296387310050C433 /* SaveToPhotosActivity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SaveToPhotosActivity.swift; sourceTree = ""; }; @@ -1490,7 +1488,6 @@ D691296D2BA75ACF005C58ED /* PrivacyInfo.xcprivacy */, D671A6BE299DA96100A81FEA /* Tusker-Bridging-Header.h */, D6D4DDCF212518A000E1C4BB /* AppDelegate.swift */, - D6CA6A91249FAD8900AD45C1 /* AudioSessionHelper.swift */, D6D79F582A13293200AB2315 /* BackgroundManager.swift */, D69261262BB3BA610023152C /* Box.swift */, D61F75B6293C119700C0B37F /* Filterer.swift */, @@ -2019,7 +2016,6 @@ D6934F322BA7E43E002B1C8D /* LoadingGalleryContentViewController.swift in Sources */, D68E525B24A3D77E0054355A /* TuskerRootViewController.swift in Sources */, D62D2422217AA7E1005076CC /* UserActivityManager.swift in Sources */, - D6CA6A92249FAD8900AD45C1 /* AudioSessionHelper.swift in Sources */, D68A76E329524D2A001DA1B3 /* ListMO.swift in Sources */, D63CC70C2910AADB000E19DE /* TuskerSceneDelegate.swift in Sources */, D61F759D2938574B00C0B37F /* FilterRow.swift in Sources */, diff --git a/Tusker/AppDelegate.swift b/Tusker/AppDelegate.swift index fb9cba58..51b060c0 100644 --- a/Tusker/AppDelegate.swift +++ b/Tusker/AppDelegate.swift @@ -35,11 +35,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate { AppShortcutItem.createItems(for: application) - DispatchQueue.global(qos: .userInitiated).async { - AudioSessionHelper.disable() - AudioSessionHelper.setDefault() - } - if let oldSavedData = SavedDataManager.load() { do { for account in oldSavedData.accountIDs { diff --git a/Tusker/AudioSessionHelper.swift b/Tusker/AudioSessionHelper.swift deleted file mode 100644 index 10510588..00000000 --- a/Tusker/AudioSessionHelper.swift +++ /dev/null @@ -1,28 +0,0 @@ -// -// AudioSessionHelper.swift -// Tusker -// -// Created by Shadowfacts on 6/21/20. -// Copyright © 2020 Shadowfacts. All rights reserved. -// - -import Foundation -import AVFoundation - -struct AudioSessionHelper { - static func enable() { - try? AVAudioSession.sharedInstance().setActive(true, options: []) - } - - static func disable() { - try? AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation) - } - - static func setDefault() { - try? AVAudioSession.sharedInstance().setCategory(.playback, options: .mixWithOthers) - } - - static func setVideoPlayback() { - try? AVAudioSession.sharedInstance().setCategory(.playback, options: []) - } -} diff --git a/Tusker/Screens/Gallery/VideoGalleryContentViewController.swift b/Tusker/Screens/Gallery/VideoGalleryContentViewController.swift index e022fc1a..a2ff8187 100644 --- a/Tusker/Screens/Gallery/VideoGalleryContentViewController.swift +++ b/Tusker/Screens/Gallery/VideoGalleryContentViewController.swift @@ -155,14 +155,27 @@ class VideoGalleryContentViewController: UIViewController, GalleryContentViewCon } func galleryContentDidAppear() { - if isFirstAppearance { - isFirstAppearance = false - player.play() + let wasFirstAppearance = isFirstAppearance + isFirstAppearance = false + + DispatchQueue.global(qos: .userInitiated).async { + try? AVAudioSession.sharedInstance().setCategory(.playback) + try? AVAudioSession.sharedInstance().setActive(true) + + if wasFirstAppearance { + DispatchQueue.main.async { + self.player.play() + } + } } } func galleryContentWillDisappear() { player.pause() + + DispatchQueue.global(qos: .userInitiated).async { + try? AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation) + } } }