NCViewerAVPlayerViewController.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // NCViewerAVPlayerViewController.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. import NCCommunication
  26. protocol NCViewerAVPlayerViewControllerDelegate {
  27. func startPictureInPicture(metadata: tableMetadata)
  28. func stopPictureInPicture(metadata: tableMetadata, playing: Bool)
  29. }
  30. class NCViewerAVPlayerViewController: AVPlayerViewController {
  31. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. var metadata = tableMetadata()
  33. var pictureInPicture: Bool = false
  34. var imageBackground: UIImage?
  35. var delegateViewerVideo: NCViewerAVPlayerViewControllerDelegate?
  36. private var rateObserverToken: Any?
  37. // MARK: - View Life Cycle
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40. delegate = self
  41. }
  42. override func viewDidAppear(_ animated: Bool) {
  43. super.viewDidAppear(animated)
  44. NCKTVHTTPCache.shared.startProxy(user: appDelegate.user, password: appDelegate.password, metadata: metadata)
  45. func play(url: URL) {
  46. player = AVPlayer(url: url)
  47. if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  48. let imageView = UIImageView.init(image: imageBackground)
  49. imageView.translatesAutoresizingMaskIntoConstraints = false
  50. contentOverlayView?.addSubview(imageView)
  51. if let view = contentOverlayView {
  52. NSLayoutConstraint.activate([
  53. imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
  54. imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  55. imageView.heightAnchor.constraint(equalToConstant: view.frame.height/3),
  56. imageView.widthAnchor.constraint(equalToConstant: view.frame.height/3),
  57. ])
  58. }
  59. }
  60. // At end go back to start
  61. NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player?.currentItem, queue: .main) { (notification) in
  62. if let item = notification.object as? AVPlayerItem, let currentItem = self.player?.currentItem, item == currentItem {
  63. self.player?.seek(to: CMTime.zero)
  64. }
  65. }
  66. rateObserverToken = player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  67. player?.play()
  68. if let time = NCManageDatabase.shared.getVideoTime(metadata: self.metadata) {
  69. player?.seek(to: time)
  70. }
  71. player?.isMuted = CCUtility.getAudioMute()
  72. // AIRPLAY
  73. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  74. player?.allowsExternalPlayback = true
  75. } else {
  76. player?.allowsExternalPlayback = false
  77. }
  78. }
  79. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  80. play(url: URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)))
  81. } else {
  82. /*
  83. NCCommunication.shared.getDirectDownload(fileId: metadata.fileId) { account, url, errorCode, errorDescription in
  84. if let url = URL(string: url) {
  85. play(url: url)
  86. }
  87. }
  88. */
  89. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  90. play(url: url)
  91. }
  92. }
  93. }
  94. override func viewWillDisappear(_ animated: Bool) {
  95. super.viewWillDisappear(animated)
  96. NCManageDatabase.shared.addVideoTime(metadata: metadata, time: player?.currentTime())
  97. if let player = self.player {
  98. CCUtility.setAudioMute(player.isMuted)
  99. }
  100. if !pictureInPicture {
  101. player?.pause()
  102. if rateObserverToken != nil {
  103. player?.removeObserver(self, forKeyPath: "rate")
  104. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  105. NCKTVHTTPCache.shared.stopProxy(metadata: metadata)
  106. self.rateObserverToken = nil
  107. }
  108. }
  109. }
  110. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  111. if keyPath != nil && keyPath == "rate" {
  112. NCKTVHTTPCache.shared.saveCache(metadata: metadata)
  113. }
  114. }
  115. }
  116. extension NCViewerAVPlayerViewController: AVPlayerViewControllerDelegate {
  117. func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_ playerViewController: AVPlayerViewController) -> Bool {
  118. true
  119. }
  120. func playerViewControllerDidStartPictureInPicture(_ playerViewController: AVPlayerViewController) {
  121. pictureInPicture = true
  122. delegateViewerVideo?.startPictureInPicture(metadata: metadata)
  123. }
  124. func playerViewControllerWillStopPictureInPicture(_ playerViewController: AVPlayerViewController) {
  125. NCManageDatabase.shared.addVideoTime(metadata: metadata, time: player?.currentTime())
  126. pictureInPicture = false
  127. let playing = player?.timeControlStatus == .playing
  128. delegateViewerVideo?.stopPictureInPicture(metadata: metadata, playing: playing)
  129. }
  130. }