Просмотр исходного кода

Remote Command Center

Signed-off-by: marinofaggiana <marino@marinofaggiana.com>
marinofaggiana 3 лет назад
Родитель
Сommit
7c7af3d894

+ 0 - 2
iOSClient/Viewer/NCViewerMedia/NCPlayer/NCPlayer.swift

@@ -139,7 +139,6 @@ class NCPlayer: NSObject {
         })
         
         NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
-        
         NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
     }
 
@@ -147,7 +146,6 @@ class NCPlayer: NSObject {
         print("deinit NCPlayer")
         
         NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
-
         NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
 
         videoRemoved()

+ 59 - 0
iOSClient/Viewer/NCViewerMedia/NCPlayer/NCPlayerToolBar.swift

@@ -25,6 +25,7 @@ import Foundation
 import NCCommunication
 import CoreMedia
 import UIKit
+import MediaPlayer
 
 class NCPlayerToolBar: UIView {
     
@@ -379,3 +380,61 @@ class NCPlayerToolBar: UIView {
         reStartTimerAutoHide()
     }
 }
+
+extension NCPlayerToolBar {
+    
+    func setupRemoteTransportControls() {
+        guard let ncplayer = ncplayer else { return }
+
+        let commandCenter = MPRemoteCommandCenter.shared()
+      
+        // Add handler for Play Command
+        commandCenter.playCommand.addTarget { event in
+            
+            if !ncplayer.isPlay() {
+                ncplayer.playerPlay()
+                return .success
+            }
+            return .commandFailed
+        }
+      
+        // Add handler for Pause Command
+        commandCenter.pauseCommand.addTarget { event in
+          
+            if ncplayer.isPlay() {
+                ncplayer.playerPause()
+                return .success
+            }
+            return .commandFailed
+        }
+    }
+    
+    func setupNowPlaying() {
+        
+        var nowPlayingInfo = [String : Any]()
+        nowPlayingInfo[MPMediaItemPropertyTitle] = metadata?.fileNameView
+      
+//        if let image = UIImage(named: "artist") {
+//            nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { size in
+//                return image
+//            }
+//        }
+        
+        nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = appDelegate.player?.currentTime
+        nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = appDelegate.player?.currentItem?.asset.duration
+        nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = appDelegate.player?.rate
+      
+        MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
+    }
+    
+    func updateNowPlaying(isPause: Bool) {
+
+        var nowPlayingInfo = MPNowPlayingInfoCenter.default().nowPlayingInfo!
+      
+        nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = appDelegate.player?.currentTime
+        nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = isPause ? 0 : 1
+      
+        // Set the metadata
+        MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
+    }
+}