marinofaggiana 3 жил өмнө
parent
commit
fe763c0d58

+ 3 - 0
iOSClient/AppDelegate.swift

@@ -63,6 +63,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
     var pasteboardOcIds: [String] = []
     var shares: [tableShare] = []
     var timerErrorNetworking: Timer?
+    
+    var videoLayer: AVPlayerLayer?
+    var player: AVPlayer?
 
     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
         

+ 23 - 25
iOSClient/Viewer/NCViewerVideo/NCViewerVideo.swift

@@ -27,14 +27,12 @@ import NCCommunication
 class NCViewerVideo: NSObject {
     
     private let appDelegate = UIApplication.shared.delegate as! AppDelegate
-    private var videoLayer: AVPlayerLayer?
     private var view: UIView?
     private var timeObserver: Any?
     private var rateObserver: Any?
     private var metadata: tableMetadata?
     
     public var viewerVideoToolBar: NCViewerVideoToolBar?
-    public var player: AVPlayer?
     public var pictureInPictureOcId: String = ""
     
     init(view: UIView?, viewerVideoToolBar: NCViewerVideoToolBar?) {
@@ -56,7 +54,7 @@ class NCViewerVideo: NSObject {
     @objc func applicationDidEnterBackground(_ notification: NSNotification) {
         
         if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
-            player?.pause()
+            appDelegate.player?.pause()
         }
     }
     
@@ -67,20 +65,20 @@ class NCViewerVideo: NSObject {
         
         func play(url: URL) {
             
-            self.player = AVPlayer(url: url)
-            self.player?.isMuted = CCUtility.getAudioMute()
-            self.videoLayer = AVPlayerLayer(player: self.player)
+            appDelegate.player = AVPlayer(url: url)
+            appDelegate.player?.isMuted = CCUtility.getAudioMute()
+            appDelegate.videoLayer = AVPlayerLayer(player: appDelegate.player)
 
             if let view = view  {
 
-                self.videoLayer!.frame = view.bounds
-                self.videoLayer!.videoGravity = .resizeAspect
-                view.layer.addSublayer(self.videoLayer!)
+                appDelegate.videoLayer!.frame = view.bounds
+                appDelegate.videoLayer!.videoGravity = .resizeAspect
+                view.layer.addSublayer(appDelegate.videoLayer!)
                 
                 // At end go back to start & show toolbar
-                NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem, queue: .main) { (notification) in
-                    if let item = notification.object as? AVPlayerItem, let currentItem = self.player?.currentItem, item == currentItem {
-                        self.player?.seek(to: .zero)
+                NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: appDelegate.player?.currentItem, queue: .main) { (notification) in
+                    if let item = notification.object as? AVPlayerItem, let currentItem = self.appDelegate.player?.currentItem, item == currentItem {
+                        self.appDelegate.player?.seek(to: .zero)
                         if metadata.livePhoto {
                             NCManageDatabase.shared.deleteVideoTime(metadata: metadata)
                         }
@@ -88,14 +86,14 @@ class NCViewerVideo: NSObject {
                     }
                 }
                             
-                self.rateObserver = self.player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
+                self.rateObserver = appDelegate.player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
                 
                 if self.pictureInPictureOcId != metadata.ocId {
-                    self.player?.play()
+                    appDelegate.player?.play()
                 }
             }
             
-            viewerVideoToolBar?.setBarPlayer(player: player)
+            viewerVideoToolBar?.setBarPlayer()
         }
         
         //NCNetworking.shared.getVideoUrl(metadata: metadata) { url in
@@ -111,22 +109,22 @@ class NCViewerVideo: NSObject {
         
         guard let metadata = self.metadata else { return }
         
-        player?.pause()
-        player?.seek(to: CMTime.zero)
+        appDelegate.player?.pause()
+        appDelegate.player?.seek(to: CMTime.zero)
         
         if let timeObserver = timeObserver {
-            player?.removeTimeObserver(timeObserver)
+            appDelegate.player?.removeTimeObserver(timeObserver)
             self.timeObserver = nil
         }
         
         if rateObserver != nil {
-            player?.removeObserver(self, forKeyPath: "rate")
+            appDelegate.player?.removeObserver(self, forKeyPath: "rate")
             NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
             NCKTVHTTPCache.shared.stopProxy(metadata: metadata)
             self.rateObserver = nil
         }
                
-        videoLayer?.removeFromSuperlayer()
+        appDelegate.videoLayer?.removeFromSuperlayer()
     }
     
     override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
@@ -137,20 +135,20 @@ class NCViewerVideo: NSObject {
             
             self.viewerVideoToolBar?.setToolBar()
             
-            if ((player?.rate) == 1) {
+            if ((appDelegate.player?.rate) == 1) {
                 
                 if let time = NCManageDatabase.shared.getVideoTime(metadata: metadata) {
-                    player?.seek(to: time)
-                    player?.isMuted = CCUtility.getAudioMute()
+                    appDelegate.player?.seek(to: time)
+                    appDelegate.player?.isMuted = CCUtility.getAudioMute()
                 }
                 
             } else if !metadata.livePhoto {
                 
-                if let time = player?.currentTime(), let duration = self.player?.currentItem?.asset.duration {
+                if let time = appDelegate.player?.currentTime(), let duration = appDelegate.player?.currentItem?.asset.duration {
                     let timeSecond = Double(CMTimeGetSeconds(time))
                     let durationSeconds = Double(CMTimeGetSeconds(duration))
                     if timeSecond < durationSeconds {
-                        NCManageDatabase.shared.addVideoTime(metadata: metadata, time: player?.currentTime())
+                        NCManageDatabase.shared.addVideoTime(metadata: metadata, time: appDelegate.player?.currentTime())
                     }
                 }
             }

+ 18 - 19
iOSClient/Viewer/NCViewerVideo/NCViewerVideoToolBar.swift

@@ -40,7 +40,7 @@ class NCViewerVideoToolBar: UIView {
         case moved
     }
     
-    var player: AVPlayer?
+    private let appDelegate = UIApplication.shared.delegate as! AppDelegate
     private var playbackSliderEvent: sliderEventType = .ended
     private let seekDuration: Float64 = 15
     private var timerAutoHide: Timer?
@@ -80,10 +80,9 @@ class NCViewerVideoToolBar: UIView {
         }
     }
     
-    func setBarPlayer(player: AVPlayer?) {
-        self.player = player
+    func setBarPlayer() {
         
-        let duration: CMTime = (player?.currentItem?.asset.duration)!
+        let duration: CMTime = (appDelegate.player?.currentItem?.asset.duration)!
         let durationSeconds: Float64 = CMTimeGetSeconds(duration)
         
         playbackSlider.value = 0
@@ -94,9 +93,9 @@ class NCViewerVideoToolBar: UIView {
         labelCurrentTime.text = stringFromTimeInterval(interval: 0)
         labelOverallDuration.text = "-" + stringFromTimeInterval(interval: durationSeconds)
         
-        player?.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, preferredTimescale: 1), queue: .main, using: { (CMTime) in
+        appDelegate.player?.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, preferredTimescale: 1), queue: .main, using: { (CMTime) in
             
-            if self.player?.currentItem?.status == .readyToPlay {
+            if self.appDelegate.player?.currentItem?.status == .readyToPlay {
                 if self.isHidden == false {
                     self.updateOutlet()
                 }
@@ -116,7 +115,7 @@ class NCViewerVideoToolBar: UIView {
             timerAutoHide = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(autoHideToolBar), userInfo: nil, repeats: true)
             return
         }
-        if self.player?.rate == 1 {
+        if appDelegate.player?.rate == 1 {
             self.isHidden = true
         }
     }
@@ -136,7 +135,7 @@ class NCViewerVideoToolBar: UIView {
     
     public func setToolBar() {
 
-        if player?.rate == 1 {
+        if appDelegate.player?.rate == 1 {
             playButton.setImage(NCUtility.shared.loadImage(named: "pause.fill", color: .white), for: .normal)
         } else {
             playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .white), for: .normal)
@@ -151,7 +150,7 @@ class NCViewerVideoToolBar: UIView {
     
     private func updateOutlet() {
         
-        if let duration = player?.currentItem?.asset.duration, let currentTime = player?.currentTime() {
+        if let duration = appDelegate.player?.currentItem?.asset.duration, let currentTime = appDelegate.player?.currentTime() {
             
             let durationSeconds: Float64 = CMTimeGetSeconds(duration)
             let currentSeconds: Float64 = CMTimeGetSeconds(currentTime)
@@ -172,9 +171,9 @@ class NCViewerVideoToolBar: UIView {
             case .moved:
                 let seconds: Int64 = Int64(self.playbackSlider.value)
                 let targetTime: CMTime = CMTimeMake(value: seconds, timescale: 1)
-                self.player?.seek(to: targetTime)
-                if self.player?.rate == 0 {
-                    self.player?.play()
+                appDelegate.player?.seek(to: targetTime)
+                if appDelegate.player?.rate == 0 {
+                    appDelegate.player?.play()
                 }
                 playbackSliderEvent = .moved
             case .ended:
@@ -189,10 +188,10 @@ class NCViewerVideoToolBar: UIView {
     
     @IBAction func playerPause(_ sender: Any) {
         
-        if player?.timeControlStatus == .playing {
-            player?.pause()
-        } else if player?.timeControlStatus == .paused {
-            player?.play()
+        if appDelegate.player?.timeControlStatus == .playing {
+            appDelegate.player?.pause()
+        } else if appDelegate.player?.timeControlStatus == .paused {
+            appDelegate.player?.play()
         }
     }
         
@@ -201,12 +200,12 @@ class NCViewerVideoToolBar: UIView {
         let mute = CCUtility.getAudioMute()
         
         CCUtility.setAudioMute(!mute)
-        player?.isMuted = !mute
+        appDelegate.player?.isMuted = !mute
         setToolBar()
     }
     
     @IBAction func forwardButtonSec(_ sender: Any) {
-        guard let player = self.player else { return }
+        guard let player = appDelegate.player else { return }
         
         if let duration = player.currentItem?.duration {
             
@@ -223,7 +222,7 @@ class NCViewerVideoToolBar: UIView {
     }
     
     @IBAction func backButtonSec(_ sender: Any) {
-        guard let player = self.player else { return }
+        guard let player = appDelegate.player else { return }
 
         let playerCurrenTime = CMTimeGetSeconds(player.currentTime())
         var newTime = playerCurrenTime - seekDuration