NCViewerVideo.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // NCViewerVideo.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 27/10/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import AVKit
  25. protocol NCViewerVideoDelegate {
  26. func startPictureInPicture(metadata: tableMetadata)
  27. func stopPictureInPicture(metadata: tableMetadata, playing: Bool)
  28. }
  29. class NCViewerVideo: AVPlayerViewController {
  30. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. var metadata = tableMetadata()
  32. var pictureInPicture: Bool = false
  33. var delegateViewerVideo: NCViewerVideoDelegate?
  34. private var rateObserverToken: Any?
  35. // MARK: - View Life Cycle
  36. override func viewDidLoad() {
  37. super.viewDidLoad()
  38. delegate = self
  39. }
  40. override func viewDidAppear(_ animated: Bool) {
  41. super.viewDidAppear(animated)
  42. NCKTVHTTPCache.shared.startProxy(user: appDelegate.user, password: appDelegate.password, metadata: metadata)
  43. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  44. player = AVPlayer(url: url)
  45. // At end go back to start
  46. NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player?.currentItem, queue: .main) { (notification) in
  47. if let item = notification.object as? AVPlayerItem, let currentItem = self.player?.currentItem, item == currentItem {
  48. self.player?.seek(to: CMTime.zero)
  49. }
  50. }
  51. rateObserverToken = player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  52. player?.play()
  53. if let time = NCManageDatabase.shared.getVideoTime(metadata: self.metadata) {
  54. player?.seek(to: time)
  55. }
  56. player?.isMuted = CCUtility.getAudioMute()
  57. }
  58. }
  59. override func viewWillDisappear(_ animated: Bool) {
  60. super.viewWillDisappear(animated)
  61. NCManageDatabase.shared.addVideoTime(metadata: metadata, time: player?.currentTime())
  62. if let player = self.player {
  63. CCUtility.setAudioMute(player.isMuted)
  64. }
  65. if !pictureInPicture {
  66. player?.pause()
  67. if rateObserverToken != nil {
  68. player?.removeObserver(self, forKeyPath: "rate")
  69. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  70. NCKTVHTTPCache.shared.stopProxy()
  71. self.rateObserverToken = nil
  72. }
  73. }
  74. }
  75. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  76. if keyPath != nil && keyPath == "rate" {
  77. NCKTVHTTPCache.shared.saveCache(metadata: metadata)
  78. }
  79. }
  80. }
  81. extension NCViewerVideo: AVPlayerViewControllerDelegate {
  82. func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_ playerViewController: AVPlayerViewController) -> Bool {
  83. true
  84. }
  85. func playerViewControllerDidStartPictureInPicture(_ playerViewController: AVPlayerViewController) {
  86. pictureInPicture = true
  87. delegateViewerVideo?.startPictureInPicture(metadata: metadata)
  88. }
  89. func playerViewControllerWillStopPictureInPicture(_ playerViewController: AVPlayerViewController) {
  90. NCManageDatabase.shared.addVideoTime(metadata: metadata, time: player?.currentTime())
  91. pictureInPicture = false
  92. let playing = player?.timeControlStatus == .playing
  93. delegateViewerVideo?.stopPictureInPicture(metadata: metadata, playing: playing)
  94. }
  95. }