123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- import UIKit
- import AVKit
- protocol NCViewerVideoDelegate {
- func startPictureInPicture(metadata: tableMetadata)
- func stopPictureInPicture(metadata: tableMetadata, playing: Bool)
- }
- class NCViewerVideo: AVPlayerViewController {
-
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
- var metadata = tableMetadata()
- var pictureInPicture: Bool = false
- var delegateViewerVideo: NCViewerVideoDelegate?
- private var rateObserverToken: Any?
- override func viewDidLoad() {
- super.viewDidLoad()
-
- delegate = self
- }
-
- override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
-
- NCKTVHTTPCache.shared.startProxy(user: appDelegate.user, password: appDelegate.password, metadata: metadata)
-
- if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
-
- player = AVPlayer(url: url)
-
-
- NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player?.currentItem, queue: .main) { (notification) in
- if let item = notification.object as? AVPlayerItem, let currentItem = self.player?.currentItem, item == currentItem {
- self.player?.seek(to: CMTime.zero)
- }
- }
-
- rateObserverToken = player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
-
- player?.play()
- if let time = NCManageDatabase.shared.getVideoTime(metadata: self.metadata) {
- player?.seek(to: time)
- }
- player?.isMuted = CCUtility.getAudioMute()
- }
- }
-
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
-
- NCManageDatabase.shared.addVideoTime(metadata: metadata, time: player?.currentTime())
-
- if let player = self.player {
- CCUtility.setAudioMute(player.isMuted)
- }
-
- if !pictureInPicture {
- player?.pause()
-
- if rateObserverToken != nil {
- player?.removeObserver(self, forKeyPath: "rate")
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
- NCKTVHTTPCache.shared.stopProxy()
- self.rateObserverToken = nil
- }
- }
- }
-
- override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
-
- if keyPath != nil && keyPath == "rate" {
- NCKTVHTTPCache.shared.saveCache(metadata: metadata)
- }
- }
- }
- extension NCViewerVideo: AVPlayerViewControllerDelegate {
-
- func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_ playerViewController: AVPlayerViewController) -> Bool {
- true
- }
-
- func playerViewControllerDidStartPictureInPicture(_ playerViewController: AVPlayerViewController) {
- pictureInPicture = true
- delegateViewerVideo?.startPictureInPicture(metadata: metadata)
- }
-
- func playerViewControllerWillStopPictureInPicture(_ playerViewController: AVPlayerViewController) {
- NCManageDatabase.shared.addVideoTime(metadata: metadata, time: player?.currentTime())
- pictureInPicture = false
- let playing = player?.timeControlStatus == .playing
- delegateViewerVideo?.stopPictureInPicture(metadata: metadata, playing: playing)
- }
- }
|