marinofaggiana 3 年之前
父節點
當前提交
e11c0ae387

+ 0 - 3
iOSClient/AppDelegate.swift

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

+ 1 - 1
iOSClient/Viewer/NCViewer.swift

@@ -41,7 +41,7 @@ class NCViewer: NSObject {
         self.metadatas = metadatas
     
         var editor = editor
-        var x = NCCommunicationCommon.shared.getInternalTypeIdentifier(typeIdentifier: metadata.contentType)
+        var xxxxxxx = NCCommunicationCommon.shared.getInternalTypeIdentifier(typeIdentifier: metadata.contentType)
         
         // IMAGE AUDIO VIDEO
         if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {

+ 12 - 23
iOSClient/Viewer/NCViewerImage/NCViewerImage.swift

@@ -54,7 +54,6 @@ class NCViewerImage: UIViewController {
     var singleTapGestureRecognizer: UITapGestureRecognizer!
     var longtapGestureRecognizer: UILongPressGestureRecognizer!
     
-    var viewerVideo: NCViewerVideo?
     var textColor: UIColor = NCBrandColor.shared.label
 
     // MARK: - View Life Cycle
@@ -130,14 +129,9 @@ class NCViewerImage: UIViewController {
     
     override func viewWillDisappear(_ animated: Bool) {
         super.viewWillDisappear(animated)
-
-        if let navigationController = self.navigationController {
-            if !navigationController.viewControllers.contains(self) {
-                self.viewerVideo?.videoStop()
-            }
-        }
+           
+        NCViewerVideo.shared.videoStop()
         
-        //
         NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
         NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
         NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
@@ -252,7 +246,7 @@ class NCViewerImage: UIViewController {
     }
     
     //
-    // Detect for LIVE
+    // Detect for LIVE PHOTO
     //
     @objc func didLongpressGestureEvent(gestureRecognizer: UITapGestureRecognizer) {
           
@@ -270,7 +264,7 @@ class NCViewerImage: UIViewController {
                 if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
                     
                     AudioServicesPlaySystemSound(1519) // peek feedback
-                    self.viewerVideo?.videoPlay(metadata: metadata)
+                    NCViewerVideo.shared.videoPlay(view: self.currentViewerImageZoom?.imageView, viewerVideoToolBar: nil, metadata: metadata)
                     
                 } else {
                     
@@ -295,7 +289,7 @@ class NCViewerImage: UIViewController {
                             
                             if gestureRecognizer.state == .changed || gestureRecognizer.state == .began {
                                 AudioServicesPlaySystemSound(1519) // peek feedback
-                                self.viewerVideo?.videoPlay(metadata: metadata)
+                                NCViewerVideo.shared.videoPlay(view: self.currentViewerImageZoom?.imageView, viewerVideoToolBar: nil, metadata: metadata)
                             }
                         }
                     }
@@ -306,7 +300,7 @@ class NCViewerImage: UIViewController {
             
             currentViewerImageZoom?.statusViewImage.isHidden = false
             currentViewerImageZoom?.statusLabel.isHidden = false
-            self.viewerVideo?.videoStop()
+            NCViewerVideo.shared.videoStop()
         }
     }
     
@@ -461,6 +455,8 @@ extension NCViewerImage: UIPageViewControllerDelegate, UIPageViewControllerDataS
         
         guard let nextViewController = pendingViewControllers.first as? NCViewerImageZoom else { return }
         nextIndex = nextViewController.index
+        // STOP VIDEO
+        NCViewerVideo.shared.videoStop()
     }
     
     func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
@@ -540,7 +536,7 @@ extension NCViewerImage: UIGestureRecognizerDelegate {
                 appDelegate.activeViewerAVPlayerViewController?.delegateViewerVideo = self
                 if let currentViewerVideo = appDelegate.activeViewerAVPlayerViewController {
                     present(currentViewerVideo, animated: false) { }
-                    self.viewerVideo?.videoStop()
+                    
                 }
             }
             
@@ -592,24 +588,17 @@ extension NCViewerImage: NCViewerImageZoomDelegate {
     func dismissImageZoom() {
         self.navigationController?.popViewController(animated: true)
     }
-    
-    func willAppearImageZoom(viewerImageZoom: NCViewerImageZoom, metadata: tableMetadata) {
-        self.viewerVideo?.videoStop()
-    }
-    
+        
     func didAppearImageZoom(viewerImageZoom: NCViewerImageZoom, metadata: tableMetadata) {
                 
         navigationItem.title = metadata.fileNameView
         currentMetadata = metadata
         currentViewerImageZoom = viewerImageZoom
         viewerImageZoom.videoToolBar.hideToolBar()
-        viewerVideo = NCViewerVideo.init(view: viewerImageZoom.imageView, viewerVideoToolBar: viewerImageZoom.videoToolBar)
         
+        // PLAY VIDEO/AUDIO
         if (currentMetadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || currentMetadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue) {
-            DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
-                self.viewerVideo?.videoPlay(metadata: metadata)
-            }
-//            viewerImageZoom.videoToolBar.showToolBar(metadata: currentMetadata)
+            NCViewerVideo.shared.videoPlay(view: viewerImageZoom.imageView, viewerVideoToolBar: viewerImageZoom.videoToolBar, metadata: metadata)
         }
             
         if !NCOperationQueue.shared.downloadExists(metadata: metadata) {

+ 0 - 3
iOSClient/Viewer/NCViewerImage/NCViewerImageZoom.swift

@@ -27,7 +27,6 @@ import NCCommunication
 protocol NCViewerImageZoomDelegate {
     func photoPageViewController(_ viewerImageZoom: NCViewerImageZoom, scrollViewDidScroll scrollView: UIScrollView)
     func didAppearImageZoom(viewerImageZoom: NCViewerImageZoom, metadata: tableMetadata)
-    func willAppearImageZoom(viewerImageZoom: NCViewerImageZoom, metadata: tableMetadata)
     func dismissImageZoom()
 }
 
@@ -108,8 +107,6 @@ class NCViewerImageZoom: UIViewController {
     override func viewWillAppear(_ animated: Bool) {
         super.viewWillAppear(animated)
             
-        delegate?.willAppearImageZoom(viewerImageZoom: self, metadata: metadata)
-        
         if isShowDetail {
             openDetail()
         }

+ 46 - 50
iOSClient/Viewer/NCViewerVideo/NCViewerVideo.swift

@@ -25,75 +25,71 @@ import Foundation
 import NCCommunication
 
 class NCViewerVideo: NSObject {
+    @objc static let shared: NCViewerVideo = {
+        let instance = NCViewerVideo()
+        NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
+        return instance
+    }()
     
     private let appDelegate = UIApplication.shared.delegate as! AppDelegate
     private var view: UIView?
     private var timeObserver: Any?
     private var rateObserver: Any?
     private var metadata: tableMetadata?
+    private var videoLayer: AVPlayerLayer?
+    private var player: AVPlayer?
     
     public var viewerVideoToolBar: NCViewerVideoToolBar?
     public var pictureInPictureOcId: String = ""
     
-    init(view: UIView?, viewerVideoToolBar: NCViewerVideoToolBar?) {
-        super.init()
-
-        self.view = view
-        self.viewerVideoToolBar = viewerVideoToolBar
-        
-        NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
-    }
-    
-    deinit {
-        
-        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
-    }
     
     //MARK: - NotificationCenter
 
     @objc func applicationDidEnterBackground(_ notification: NSNotification) {
         
         if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
-            appDelegate.player?.pause()
+            self.player?.pause()
         }
     }
     
-    func videoPlay(metadata: tableMetadata) {
+    func videoPlay(view: UIView?, viewerVideoToolBar: NCViewerVideoToolBar?, metadata: tableMetadata) {
+        guard let view = view else { return }
+        
+        self.view = view
+        self.viewerVideoToolBar = viewerVideoToolBar
         self.metadata = metadata
         
         NCKTVHTTPCache.shared.startProxy(user: appDelegate.user, password: appDelegate.password, metadata: metadata)
         
         func play(url: URL) {
             
-            appDelegate.player = AVPlayer(url: url)
-            appDelegate.player?.isMuted = CCUtility.getAudioMute()
-            appDelegate.videoLayer = AVPlayerLayer(player: appDelegate.player)
-
-            if let view = view  {
-
-                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: 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)
-                        }
-                        self.viewerVideoToolBar?.showToolBar(metadata: metadata)
+            self.player = AVPlayer(url: url)
+            self.player?.isMuted = CCUtility.getAudioMute()
+            self.videoLayer = AVPlayerLayer(player: self.player)
+            self.videoLayer!.frame = view.bounds
+            self.videoLayer!.videoGravity = .resizeAspect
+            
+            view.layer.addSublayer(self.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)
+                    if metadata.livePhoto {
+                        NCManageDatabase.shared.deleteVideoTime(metadata: metadata)
                     }
-                }
-                            
-                self.rateObserver = appDelegate.player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
-                
-                if self.pictureInPictureOcId != metadata.ocId {
-                    appDelegate.player?.play()
+                    self.viewerVideoToolBar?.showToolBar(metadata: metadata)
                 }
             }
+                        
+            self.rateObserver = self.player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
+            
+            if self.pictureInPictureOcId != metadata.ocId {
+                self.player?.play()
+            }
+            
             
-            viewerVideoToolBar?.setBarPlayer()
+            viewerVideoToolBar?.setBarPlayer(player: self.player)
         }
         
         //NCNetworking.shared.getVideoUrl(metadata: metadata) { url in
@@ -109,22 +105,22 @@ class NCViewerVideo: NSObject {
         
         guard let metadata = self.metadata else { return }
         
-        appDelegate.player?.pause()
-        appDelegate.player?.seek(to: CMTime.zero)
+        self.player?.pause()
+        self.player?.seek(to: CMTime.zero)
         
         if let timeObserver = timeObserver {
-            appDelegate.player?.removeTimeObserver(timeObserver)
+            self.player?.removeTimeObserver(timeObserver)
             self.timeObserver = nil
         }
         
         if rateObserver != nil {
-            appDelegate.player?.removeObserver(self, forKeyPath: "rate")
+            self.player?.removeObserver(self, forKeyPath: "rate")
             NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
             NCKTVHTTPCache.shared.stopProxy(metadata: metadata)
             self.rateObserver = nil
         }
                
-        appDelegate.videoLayer?.removeFromSuperlayer()
+        self.videoLayer?.removeFromSuperlayer()
     }
     
     override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
@@ -135,20 +131,20 @@ class NCViewerVideo: NSObject {
             
             self.viewerVideoToolBar?.setToolBar()
             
-            if ((appDelegate.player?.rate) == 1) {
+            if ((self.player?.rate) == 1) {
                 
                 if let time = NCManageDatabase.shared.getVideoTime(metadata: metadata) {
-                    appDelegate.player?.seek(to: time)
-                    appDelegate.player?.isMuted = CCUtility.getAudioMute()
+                    self.player?.seek(to: time)
+                    self.player?.isMuted = CCUtility.getAudioMute()
                 }
                 
             } else if !metadata.livePhoto {
                 
-                if let time = appDelegate.player?.currentTime(), let duration = appDelegate.player?.currentItem?.asset.duration {
+                if let time = self.player?.currentTime(), let duration = self.player?.currentItem?.asset.duration {
                     let timeSecond = Double(CMTimeGetSeconds(time))
                     let durationSeconds = Double(CMTimeGetSeconds(duration))
                     if timeSecond < durationSeconds {
-                        NCManageDatabase.shared.addVideoTime(metadata: metadata, time: appDelegate.player?.currentTime())
+                        NCManageDatabase.shared.addVideoTime(metadata: metadata, time: self.player?.currentTime())
                     }
                 }
             }

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

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