NCViewerVideoAudio.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. var videoURL: URL?
  30. NCKTVHTTPCache.shared.setupHTTPCache()
  31. NCKTVHTTPCache.shared.saveCache(metadata: metadata)
  32. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  33. videoURL = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  34. } else {
  35. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
  36. return
  37. }
  38. videoURL = NCKTVHTTPCache.shared.getProxyURL(stringURL: stringURL)
  39. NCKTVHTTPCache.shared.setAuth(user: appDelegate.user, password: appDelegate.password)
  40. }
  41. if let url = videoURL {
  42. let video = AVPlayer(url: url)
  43. player = video
  44. // At end go back to start
  45. NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { (notification) in
  46. let player = notification.object as! AVPlayerItem
  47. player.seek(to: CMTime.zero, completionHandler: nil)
  48. }
  49. player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  50. player?.play()
  51. }
  52. }
  53. override func viewDidDisappear(_ animated: Bool) {
  54. super.viewDidDisappear(animated)
  55. player?.pause()
  56. removeObserver()
  57. NCKTVHTTPCache.shared.stopProxy()
  58. }
  59. //MARK: -
  60. func saveCache() {
  61. if !CCUtility.fileProviderStorageExists(self.metadata.ocId, fileNameView:self.metadata.fileNameView) {
  62. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
  63. let videoURL = URL(string: stringURL)
  64. guard let url = NCKTVHTTPCache.shared.getCompleteFileURL(videoURL: videoURL) else { return }
  65. CCUtility.copyFile(atPath: url.path, toPath: CCUtility.getDirectoryProviderStorageOcId(self.metadata.ocId, fileNameView: self.metadata.fileNameView))
  66. NCManageDatabase.sharedInstance.addLocalFile(metadata: self.metadata)
  67. NCKTVHTTPCache.shared.deleteCache(videoURL: videoURL)
  68. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["ocId":self.metadata.ocId, "serverUrl":self.metadata.serverUrl])
  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. if player?.rate == 1 {
  75. print("start")
  76. } else {
  77. print("pause")
  78. }
  79. saveCache()
  80. }
  81. }
  82. @objc func removeObserver() {
  83. player?.removeObserver(self, forKeyPath: "rate", context: nil)
  84. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  85. }
  86. }