瀏覽代碼

improved video

marinofaggiana 4 年之前
父節點
當前提交
3a1ab5b27c
共有 1 個文件被更改,包括 24 次插入19 次删除
  1. 24 19
      iOSClient/Viewer/NCViewerImage/NCViewerImagePageContainer.swift

+ 24 - 19
iOSClient/Viewer/NCViewerImage/NCViewerImagePageContainer.swift

@@ -61,7 +61,8 @@ class NCViewerImagePageContainer: UIViewController, UIGestureRecognizerDelegate
     private var playerVideo: AVPlayer?
     private var playerVideo: AVPlayer?
     private var videoLayer: AVPlayerLayer?
     private var videoLayer: AVPlayerLayer?
     private var durationVideo: Float = 0
     private var durationVideo: Float = 0
-    
+    private var timeObserverToken: Any?
+
     override func viewDidLoad() {
     override func viewDidLoad() {
         super.viewDidLoad()
         super.viewDidLoad()
         
         
@@ -513,26 +514,10 @@ class NCViewerImagePageContainer: UIViewController, UIGestureRecognizerDelegate
             videoLayer!.videoGravity = AVLayerVideoGravity.resizeAspectFill
             videoLayer!.videoGravity = AVLayerVideoGravity.resizeAspectFill
             
             
             currentViewerImageZoom!.imageView.layer.addSublayer(videoLayer!)
             currentViewerImageZoom!.imageView.layer.addSublayer(videoLayer!)
-            
-            // At end go back to start
-            NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { (notification) in
-                self.playerVideo?.seek(to: CMTime.zero)
-            }
-                        
-            playerVideo?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
-            videoPlay()
-        }
-    }
     
     
-    /*
-    @objc func updateTimer() {
-        if let durationVideo = playerVideo?.currentItem?.asset.duration {
-            timerCounter += 1
-            let floatTime = Float(CMTimeGetSeconds(durationVideo))
-            progressView.progress = timerCounter / floatTime
+            videoPlay()
         }
         }
     }
     }
-    */
     
     
     //MARK: - Action
     //MARK: - Action
     
     
@@ -561,15 +546,35 @@ class NCViewerImagePageContainer: UIViewController, UIGestureRecognizerDelegate
     }
     }
     
     
     @objc func videoPlay() {
     @objc func videoPlay() {
-        playerVideo?.play()
+        
+        let timeScale = CMTimeScale(NSEC_PER_SEC)
+        let time = CMTime(seconds: 0.5, preferredTimescale: timeScale)
+                
         if let duration = playerVideo?.currentItem?.asset.duration {
         if let duration = playerVideo?.currentItem?.asset.duration {
             durationVideo = Float(CMTimeGetSeconds(duration))
             durationVideo = Float(CMTimeGetSeconds(duration))
         }
         }
+        
+        timeObserverToken = playerVideo?.addPeriodicTimeObserver(forInterval: time, queue: .main) { [weak self] time in
+            print(time)
+        }
+        
+        // At end go back to start
+        NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: .main) { (notification) in
+            self.playerVideo?.seek(to: CMTime.zero)
+        }
+                    
+        playerVideo?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
+        
+        playerVideo?.play()
     }
     }
     
     
     @objc func videoStop() {
     @objc func videoStop() {
         playerVideo?.pause()
         playerVideo?.pause()
         self.playerVideo?.seek(to: CMTime.zero)
         self.playerVideo?.seek(to: CMTime.zero)
+        if let timeObserverToken = timeObserverToken {
+            playerVideo?.removeTimeObserver(timeObserverToken)
+            self.timeObserverToken = nil
+        }
     }
     }
 }
 }