Browse Source

improved pip

Signed-off-by: marinofaggiana <marino@marinofaggiana.com>
marinofaggiana 3 years ago
parent
commit
e94930eea0

+ 4 - 40
iOSClient/Viewer/NCViewerMedia/NCPlayer/NCPlayer.swift

@@ -25,12 +25,8 @@ import Foundation
 import NCCommunication
 import UIKit
 import AVFoundation
-import AVKit
 import MediaPlayer
 
-/// The Set of custom player controllers currently using or transitioning out of PiP
-private var activeNCPlayer = Set<NCPlayer>()
-
 class NCPlayer: NSObject {
    
     private let appDelegate = UIApplication.shared.delegate as! AppDelegate
@@ -43,7 +39,6 @@ class NCPlayer: NSObject {
     public var durationTime: CMTime = .zero
     public var metadata: tableMetadata?
     public var videoLayer: AVPlayerLayer?
-    public var pictureInPictureController: AVPictureInPictureController?
     
     // MARK: - View Life Cycle
 
@@ -193,9 +188,10 @@ class NCPlayer: NSObject {
     //MARK: - NotificationCenter
 
     @objc func applicationDidEnterBackground(_ notification: NSNotification) {
+        guard let playerToolBar = self.playerToolBar else { return }
         
         if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
-            if !isPictureInPictureActive() {
+            if !playerToolBar.isPictureInPictureActive() {
                 playerPause()
             }
         }
@@ -213,16 +209,6 @@ class NCPlayer: NSObject {
         if player?.rate == 1 { return true } else { return false }
     }
     
-    func isPictureInPictureActive() -> Bool {
-        guard let pictureInPictureController = self.pictureInPictureController else { return false }
-        
-        if pictureInPictureController.isPictureInPictureActive {
-            return true
-        } else {
-            return false
-        }
-    }
-    
     func playerPlay() {
                 
         player?.play()
@@ -234,8 +220,8 @@ class NCPlayer: NSObject {
         player?.pause()
         self.playerToolBar?.updateToolBar()
         
-        if isPictureInPictureActive() {
-            pictureInPictureController?.stopPictureInPicture()
+        if let playerToolBar = self.playerToolBar, playerToolBar.isPictureInPictureActive() {
+            playerToolBar.pictureInPictureController?.stopPictureInPicture()
         }
     }
     
@@ -300,27 +286,5 @@ class NCPlayer: NSObject {
     }
 }
 
-extension NCPlayer: AVPictureInPictureControllerDelegate {
-    
-    func pictureInPictureControllerWillStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
-        
-        activeNCPlayer.insert(self)
-    }
-    
-    func pictureInPictureControllerDidStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
-        // nothing
-    }
-    
-    func pictureInPictureControllerWillStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
-        //nothing
-    }
-    
-    func pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
-        guard let metadata = self.metadata else { return }
 
-        if !isPlay() {
-            NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId":metadata.ocId, "enableTimerAutoHide": false])
-        }
-    }
-}
 

+ 43 - 12
iOSClient/Viewer/NCViewerMedia/NCPlayer/NCPlayerToolBar.swift

@@ -54,6 +54,7 @@ class NCPlayerToolBar: UIView {
     private var metadata: tableMetadata?
     private var image: UIImage?
     
+    var pictureInPictureController: AVPictureInPictureController?
     weak var viewerMedia: NCViewerMedia?
 
     // MARK: - View Life Cycle
@@ -149,14 +150,14 @@ class NCPlayerToolBar: UIView {
         muteButton.isEnabled = true
         
         // PIP
-        if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
+        if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue && AVPictureInPictureController.isPictureInPictureSupported() {
             pipButton.setImage(NCUtility.shared.loadImage(named: "pip.enter", color: .white), for: .normal)
             pipButton.isEnabled = true
         } else {
             pipButton.setImage(NCUtility.shared.loadImage(named: "pip.enter", color: .gray), for: .normal)
             pipButton.isEnabled = false
-            ncplayer.pictureInPictureController = nil
-            ncplayer.pictureInPictureController?.delegate = nil
+            pictureInPictureController = nil
+            pictureInPictureController?.delegate = nil
         }
         
         // SLIDER TIME (START - END)
@@ -342,6 +343,16 @@ class NCPlayerToolBar: UIView {
         reStartTimerAutoHide()
     }
     
+    func isPictureInPictureActive() -> Bool {
+        guard let pictureInPictureController = self.pictureInPictureController else { return false }
+        
+        if pictureInPictureController.isPictureInPictureActive {
+            return true
+        } else {
+            return false
+        }
+    }
+    
     func forward() {
         
         var index: Int = 0
@@ -408,12 +419,15 @@ class NCPlayerToolBar: UIView {
     //MARK: - Action
     
     @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
+        // nothing
     }
     
     @IBAction func buttonPlayerToolBarTouchInside(_ sender: UIButton) {
+        // nothing
     }
     
     @IBAction func buttonPlayerTopToolBarTouchInside(_ sender: UIButton) {
+        // nothing
     }
     
     @IBAction func playerPause(_ sender: Any) {
@@ -454,16 +468,21 @@ class NCPlayerToolBar: UIView {
     
     @IBAction func setPip(_ sender: Any) {
         guard let metadata = self.metadata else { return }
+        guard let ncplayer = self.ncplayer else { return }
+        guard let videoLayer = ncplayer.videoLayer else { return }
         
-        if let ncplayer = self.ncplayer, let playerLayer = ncplayer.videoLayer {
-            if ncplayer.pictureInPictureController == nil {
-                ncplayer.pictureInPictureController = AVPictureInPictureController(playerLayer: playerLayer)
-                ncplayer.pictureInPictureController?.delegate = ncplayer
-            }
-            if let pictureInPictureController = ncplayer.pictureInPictureController, pictureInPictureController.isPictureInPicturePossible {
-                ncplayer.pictureInPictureController?.startPictureInPicture()
-                NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterHidePlayerToolBar, userInfo: ["ocId":metadata.ocId])
-            }
+        if let pictureInPictureController = self.pictureInPictureController, pictureInPictureController.isPictureInPictureActive {
+            pictureInPictureController.stopPictureInPicture()
+        }
+        
+        if pictureInPictureController == nil {
+            pictureInPictureController = AVPictureInPictureController(playerLayer: videoLayer)
+            pictureInPictureController?.delegate = self
+        }
+        
+        if let pictureInPictureController = pictureInPictureController, pictureInPictureController.isPictureInPicturePossible {
+            pictureInPictureController.startPictureInPicture()
+            NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterHidePlayerToolBar, userInfo: ["ocId":metadata.ocId])
         }
     }
     
@@ -494,3 +513,15 @@ class NCPlayerToolBar: UIView {
     }
 }
 
+extension NCPlayerToolBar: AVPictureInPictureControllerDelegate {
+    
+    func pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
+        guard let metadata = self.metadata else { return }
+        guard let ncplayer = self.ncplayer else { return }
+
+        if !ncplayer.isPlay() {
+            NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId":metadata.ocId, "enableTimerAutoHide": false])
+        }
+    }
+}
+

+ 3 - 3
iOSClient/Viewer/NCViewerMedia/NCViewerMedia.swift

@@ -323,7 +323,7 @@ class NCViewerMedia: UIViewController {
     @objc func showPlayerToolBar(_ notification: NSNotification) {
         
         if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, let enableTimerAutoHide = userInfo["enableTimerAutoHide"] as? Bool{
-            if currentViewController.metadata.ocId == ocId, let ncplayer = currentViewController.ncplayer, !ncplayer.isPictureInPictureActive() {
+            if currentViewController.metadata.ocId == ocId, let playerToolBar = currentViewController.playerToolBar, !playerToolBar.isPictureInPictureActive() {
                 changeScreenMode(mode: .normal, enableTimerAutoHide: enableTimerAutoHide)
             }
         }
@@ -631,8 +631,8 @@ extension NCViewerMedia: UIGestureRecognizerDelegate {
     
     @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
                 
-        if let ncplayer = currentViewController.ncplayer, ncplayer.isPictureInPictureActive() {
-            ncplayer.pictureInPictureController?.stopPictureInPicture()
+        if let playerToolBar = currentViewController.playerToolBar, playerToolBar.isPictureInPictureActive() {
+            playerToolBar.pictureInPictureController?.stopPictureInPicture()
         }
         
         if currentScreenMode == .full {