NCViewerVideoAudio.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // NCViewerVideoAudio.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 Foundation
  24. class NCViewerVideoAudio: AVPlayerViewController {
  25. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  26. var metadata = tableMetadata()
  27. var seekTime: CMTime?
  28. weak var delegateViewerImage: NCViewerImage?
  29. private var rateObserverToken: Any?
  30. override func viewDidLoad() {
  31. super.viewDidLoad()
  32. }
  33. override func viewDidAppear(_ animated: Bool) {
  34. super.viewDidAppear(animated)
  35. var videoURL: URL?
  36. NCKTVHTTPCache.shared.startProxy(user: appDelegate.user, password: appDelegate.password, metadata: metadata)
  37. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  38. videoURL = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  39. } else {
  40. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
  41. return
  42. }
  43. allowsPictureInPicturePlayback = false
  44. videoURL = NCKTVHTTPCache.shared.getProxyURL(stringURL: stringURL)
  45. }
  46. if let url = videoURL {
  47. player = AVPlayer(url: url)
  48. // At end go back to start
  49. NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { (notification) in
  50. self.player?.seek(to: CMTime.zero)
  51. }
  52. rateObserverToken = player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  53. player?.play()
  54. if seekTime != nil {
  55. player?.seek(to: seekTime!)
  56. seekTime = nil
  57. }
  58. }
  59. }
  60. override func viewWillDisappear(_ animated: Bool) {
  61. super.viewWillDisappear(animated)
  62. player?.pause()
  63. if rateObserverToken != nil {
  64. player?.removeObserver(self, forKeyPath: "rate")
  65. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  66. NCKTVHTTPCache.shared.stopProxy()
  67. self.delegateViewerImage?.seekTime = player?.currentTime()
  68. self.rateObserverToken = nil
  69. }
  70. }
  71. //MARK: - Observer
  72. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  73. if keyPath != nil && keyPath == "rate" {
  74. NCKTVHTTPCache.shared.saveCache(metadata: metadata)
  75. }
  76. }
  77. }