NCViewerMedia.swift 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // NCViewerMedia.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 21/09/18.
  6. // Copyright © 2018 TWS. All rights reserved.
  7. //
  8. import Foundation
  9. import KTVHTTPCache
  10. class NCViewerMedia: NSObject {
  11. @objc static let sharedInstance: NCViewerMedia = {
  12. let instance = NCViewerMedia()
  13. return instance
  14. }()
  15. var viewDetail: CCDetail!
  16. var metadata: tableMetadata!
  17. var videoURL: URL!
  18. var videoURLProxy: URL!
  19. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  20. @objc func viewMedia(_ metadata: tableMetadata, viewDetail: CCDetail, width: Int, height: Int) {
  21. self.viewDetail = viewDetail
  22. self.metadata = metadata
  23. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  24. return
  25. }
  26. if CCUtility.fileProviderStorageExists(metadata.fileID, fileNameView: metadata.fileNameView) {
  27. self.videoURL = URL(string: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID, fileNameView: metadata.fileNameView))
  28. self.videoURLProxy = videoURL
  29. } else {
  30. guard let stringURL = (serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
  31. return
  32. }
  33. self.videoURL = URL(string: stringURL)
  34. self.videoURLProxy = KTVHTTPCache.proxyURL(withOriginalURL: self.videoURL)
  35. guard let authData = (appDelegate.activeUser + ":" + appDelegate.activePassword).data(using: .utf8) else {
  36. return
  37. }
  38. let authValue = "Basic " + authData.base64EncodedString(options: [])
  39. let header = [authValue:"Authorization", CCUtility.getUserAgent():"User-Agent"] as [String : String]
  40. KTVHTTPCache.downloadSetAdditionalHeaders(header)
  41. // Disable Button Action (the file is in download via Proxy Server)
  42. viewDetail.buttonAction.isEnabled = false
  43. }
  44. appDelegate.player = AVPlayer(url: videoURLProxy)
  45. appDelegate.playerController = AVPlayerViewController()
  46. appDelegate.playerController.player = appDelegate.player
  47. appDelegate.playerController.view.frame = CGRect(x: 0, y: 0, width: width, height: height)
  48. appDelegate.playerController.allowsPictureInPicturePlayback = false
  49. viewDetail.addChild(appDelegate.playerController)
  50. viewDetail.view.addSubview(appDelegate.playerController.view)
  51. appDelegate.playerController.didMove(toParent: viewDetail)
  52. NotificationCenter.default.addObserver(self, selector: #selector(self.itemDidFinishPlaying(notification:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  53. appDelegate.player.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  54. }
  55. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  56. if keyPath == "rate" {
  57. if appDelegate.player?.rate != nil {
  58. print("start")
  59. } else {
  60. print("stop")
  61. }
  62. saveCacheToFileProvider()
  63. }
  64. }
  65. @objc func itemDidFinishPlaying(notification: NSNotification) {
  66. let player = notification.object as! AVPlayerItem
  67. player.seek(to: CMTime.zero)
  68. }
  69. func saveCacheToFileProvider() {
  70. }
  71. }