diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index f46a99e7..9c180300 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -234,6 +234,8 @@ D6C7D27D22B6EBF800071952 /* AttachmentsContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6C7D27C22B6EBF800071952 /* AttachmentsContainerView.swift */; }; D6C94D872139E62700CB5196 /* LargeImageViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6C94D862139E62700CB5196 /* LargeImageViewController.swift */; }; D6C94D892139E6EC00CB5196 /* AttachmentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6C94D882139E6EC00CB5196 /* AttachmentView.swift */; }; + D6CA6A92249FAD8900AD45C1 /* AudioSessionHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6CA6A91249FAD8900AD45C1 /* AudioSessionHelper.swift */; }; + D6CA6A94249FADE700AD45C1 /* GalleryPlayerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6CA6A93249FADE700AD45C1 /* GalleryPlayerViewController.swift */; }; D6D4DDD0212518A000E1C4BB /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6D4DDCF212518A000E1C4BB /* AppDelegate.swift */; }; D6D4DDD7212518A200E1C4BB /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D6D4DDD6212518A200E1C4BB /* Assets.xcassets */; }; D6D4DDDA212518A200E1C4BB /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D6D4DDD8212518A200E1C4BB /* LaunchScreen.storyboard */; }; @@ -536,6 +538,8 @@ D6C7D27C22B6EBF800071952 /* AttachmentsContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttachmentsContainerView.swift; sourceTree = ""; }; D6C94D862139E62700CB5196 /* LargeImageViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LargeImageViewController.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 = ""; }; + D6CA6A93249FADE700AD45C1 /* GalleryPlayerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GalleryPlayerViewController.swift; sourceTree = ""; }; D6D4DDCC212518A000E1C4BB /* Tusker.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Tusker.app; sourceTree = BUILT_PRODUCTS_DIR; }; D6D4DDCF212518A000E1C4BB /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; D6D4DDD6212518A200E1C4BB /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -618,6 +622,7 @@ 04D14BAE22B34A2800642648 /* GalleryViewController.swift */, D647D92724257BEB0005044F /* AttachmentPreviewViewController.swift */, D6531DEF246B867E000F9538 /* GifvAttachmentViewController.swift */, + D6CA6A93249FADE700AD45C1 /* GalleryPlayerViewController.swift */, ); path = "Attachment Gallery"; sourceTree = ""; @@ -1289,6 +1294,7 @@ D6DFC69F242C4CCC00ACC392 /* WeakArray.swift */, D64D8CA82463B494006B0BAA /* CachedDictionary.swift */, D60E2F2B24423EAD005F8713 /* LazilyDecoding.swift */, + D6CA6A91249FAD8900AD45C1 /* AudioSessionHelper.swift */, D6F1F84E2193B9BE00F5FE67 /* Caching */, D6757A7A2157E00100721E32 /* XCallbackURL */, D62D241E217AA46B005076CC /* Shortcuts */, @@ -1725,11 +1731,13 @@ D60E2F2E244248BF005F8713 /* MastodonCachePersistentStore.swift in Sources */, D620483623D38075008A63EF /* ContentTextView.swift in Sources */, D6F2E965249E8BFD005846BB /* CrashReporterViewController.swift in Sources */, + D6CA6A94249FADE700AD45C1 /* GalleryPlayerViewController.swift in Sources */, D6A3BC802321B7E600FD64D5 /* FollowNotificationGroupTableViewCell.swift in Sources */, D627944D23A9A03D00D38C68 /* ListTimelineViewController.swift in Sources */, D6945C3823AC739F005C403C /* InstanceTimelineViewController.swift in Sources */, D60309B52419D4F100A465FF /* ComposeAttachmentsViewController.swift in Sources */, D62D2422217AA7E1005076CC /* UserActivityManager.swift in Sources */, + D6CA6A92249FAD8900AD45C1 /* AudioSessionHelper.swift in Sources */, D60D2B8223844C71001B87A3 /* BaseStatusTableViewCell.swift in Sources */, D60E2F272442372B005F8713 /* StatusMO.swift in Sources */, D60E2F2C24423EAD005F8713 /* LazilyDecoding.swift in Sources */, diff --git a/Tusker/AppDelegate.swift b/Tusker/AppDelegate.swift index 9d8ecee9..d22903ef 100644 --- a/Tusker/AppDelegate.swift +++ b/Tusker/AppDelegate.swift @@ -8,7 +8,6 @@ import UIKit import CrashReporter -import MessageUI @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { @@ -20,7 +19,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate { #if !DEBUG setupCrashReporter() #endif + AppShortcutItem.createItems(for: application) + + DispatchQueue.global(qos: .userInitiated).async { + AudioSessionHelper.disable() + AudioSessionHelper.setDefault() + } + return true } diff --git a/Tusker/AudioSessionHelper.swift b/Tusker/AudioSessionHelper.swift new file mode 100644 index 00000000..10510588 --- /dev/null +++ b/Tusker/AudioSessionHelper.swift @@ -0,0 +1,28 @@ +// +// 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/Attachment Gallery/GalleryPlayerViewController.swift b/Tusker/Screens/Attachment Gallery/GalleryPlayerViewController.swift new file mode 100644 index 00000000..c55ef87b --- /dev/null +++ b/Tusker/Screens/Attachment Gallery/GalleryPlayerViewController.swift @@ -0,0 +1,36 @@ +// +// GalleryPlayerViewController.swift +// Tusker +// +// Created by Shadowfacts on 6/21/20. +// Copyright © 2020 Shadowfacts. All rights reserved. +// + +import UIKit +import AVKit + +class GalleryPlayerViewController: AVPlayerViewController { + + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + + // starting while audio is already playing from another app often takes nearly a second, + // so do it on a background thread as to not block the UI + DispatchQueue.global(qos: .userInitiated).async { + AudioSessionHelper.enable() + AudioSessionHelper.setVideoPlayback() + } + } + + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + + // starting often takes around half a second, + // so do it on a background thread as to not block the UI + DispatchQueue.global(qos: .userInitiated).async { + AudioSessionHelper.setDefault() + AudioSessionHelper.disable() + } + } + +} diff --git a/Tusker/Screens/Attachment Gallery/GalleryViewController.swift b/Tusker/Screens/Attachment Gallery/GalleryViewController.swift index 64843017..e6053b07 100644 --- a/Tusker/Screens/Attachment Gallery/GalleryViewController.swift +++ b/Tusker/Screens/Attachment Gallery/GalleryViewController.swift @@ -72,7 +72,7 @@ class GalleryViewController: UIPageViewController, UIPageViewControllerDataSourc vc.shrinkGestureEnabled = false return vc case .video, .audio: - let vc = AVPlayerViewController() + let vc = GalleryPlayerViewController() vc.player = AVPlayer(url: attachment.url) return vc case .gifv: diff --git a/Tusker/Views/Attachments/GifvAttachmentView.swift b/Tusker/Views/Attachments/GifvAttachmentView.swift index 4de213b6..b737b2be 100644 --- a/Tusker/Views/Attachments/GifvAttachmentView.swift +++ b/Tusker/Views/Attachments/GifvAttachmentView.swift @@ -30,8 +30,9 @@ class GifvAttachmentView: UIView { playerLayer.player = player playerLayer.videoGravity = gravity + player.isMuted = true player.play() - + NotificationCenter.default.addObserver(self, selector: #selector(restartItem), name: .AVPlayerItemDidPlayToEndTime, object: item) }