|
@@ -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())
|
|
|
}
|
|
|
}
|
|
|
}
|