marinofaggiana 4 years ago
parent
commit
b4c914262b

+ 5 - 4
iOSClient/Data/NCManageDatabase.swift

@@ -2690,17 +2690,18 @@ class NCManageDatabase: NSObject {
     //MARK: -
     //MARK: Table Video
     
-    func addVideo(account: String, ocId: String, time: CMTime) {
+    func addVideoTime(account: String, ocId: String, time: CMTime?) {
         
+        guard let time = time else { return }
         let realm = try! Realm()
-
+        
         do {
             try realm.safeWrite {
                 let addObject = tableVideo()
                
                 addObject.account = account
                 addObject.ocId = ocId
-                addObject.time = Int64(CMTimeGetSeconds(time))
+                addObject.time = Int64(CMTimeGetSeconds(time) * 1000)
               
                 realm.add(addObject, update: .all)
             }
@@ -2717,7 +2718,7 @@ class NCManageDatabase: NSObject {
             return nil
         }
         
-        let time = CMTimeMake(value: result.time, timescale: 1)
+        let time = CMTimeMake(value: result.time, timescale: 1000)
         return time
     }
     

+ 1 - 3
iOSClient/Viewer/NCViewerImage/NCViewerImage.swift

@@ -481,9 +481,7 @@ class NCViewerImage: UIViewController {
                     player?.isMuted = CCUtility.getAudioMute()
                 }
             } else {
-                if let time = player?.currentTime() {
-                    NCManageDatabase.sharedInstance.addVideo(account: self.currentMetadata.account, ocId: self.currentMetadata.ocId, time: time)
-                }
+                NCManageDatabase.sharedInstance.addVideoTime(account: self.currentMetadata.account, ocId: self.currentMetadata.ocId, time: player?.currentTime())
                 print("Pause")
             }
         }

+ 6 - 19
iOSClient/Viewer/NCViewerVideo/NCViewerVideo.swift

@@ -59,6 +59,10 @@ protocol NCViewerVideoDelegate {
             rateObserverToken = player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
             
             player?.play()
+            if let time = NCManageDatabase.sharedInstance.getVideoTime(account: self.metadata.account, ocId: self.metadata.ocId) {
+                player?.seek(to: time)
+            }
+            player?.isMuted = CCUtility.getAudioMute()
         }
     }
     
@@ -76,9 +80,7 @@ protocol NCViewerVideoDelegate {
                 player?.removeObserver(self, forKeyPath: "rate")
                 NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
                 NCKTVHTTPCache.shared.stopProxy()
-                if let time = player?.currentTime() {
-                    NCManageDatabase.sharedInstance.addVideo(account: metadata.account, ocId: metadata.ocId, time: time)
-                }
+                NCManageDatabase.sharedInstance.addVideoTime(account: metadata.account, ocId: metadata.ocId, time: player?.currentTime())
                 self.rateObserverToken = nil
             }
         }
@@ -87,20 +89,7 @@ protocol NCViewerVideoDelegate {
     override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
         
         if keyPath != nil && keyPath == "rate" {
-            
             NCKTVHTTPCache.shared.saveCache(metadata: metadata)
-            
-            if ((player?.rate) == 1) {
-                if let time = NCManageDatabase.sharedInstance.getVideoTime(account: self.metadata.account, ocId: self.metadata.ocId) {
-                    player?.seek(to: time)
-                    player?.isMuted = CCUtility.getAudioMute()
-                }
-            } else {
-                if let time = player?.currentTime() {
-                    NCManageDatabase.sharedInstance.addVideo(account: self.metadata.account, ocId: self.metadata.ocId, time: time)
-                }
-                print("Pause")
-            }
         }
     }
 }
@@ -117,10 +106,8 @@ extension NCViewerVideo: AVPlayerViewControllerDelegate {
     }
     
     func playerViewControllerDidStopPictureInPicture(_ playerViewController: AVPlayerViewController) {
+        NCManageDatabase.sharedInstance.addVideoTime(account: metadata.account, ocId: metadata.ocId, time: player?.currentTime())
         pictureInPicture = false
-        if let time = player?.currentTime() {
-            NCManageDatabase.sharedInstance.addVideo(account: metadata.account, ocId: metadata.ocId, time: time)
-        }
         delegateViewerVideo?.stopPictureInPicture(metadata: metadata)
     }
 }