NCPlayer.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // NCPlayer.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 01/07/21.
  6. // Copyright © 2021 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. import NCCommunication
  25. import UIKit
  26. class NCPlayer: AVPlayer {
  27. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  28. private var imageVideoContainer: imageVideoContainerView?
  29. private var durationSeconds: Double = 0
  30. private var playerToolBar: NCPlayerToolBar?
  31. public var metadata: tableMetadata?
  32. public var videoLayer: AVPlayerLayer?
  33. func setupVideoLayer(imageVideoContainer: imageVideoContainerView?, playerToolBar: NCPlayerToolBar?, metadata: tableMetadata) {
  34. self.playerToolBar = playerToolBar
  35. self.metadata = metadata
  36. isMuted = CCUtility.getAudioMute()
  37. seek(to: .zero)
  38. // At end go back to start & show toolbar
  39. NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: currentItem, queue: .main) { (notification) in
  40. if let item = notification.object as? AVPlayerItem, let currentItem = self.currentItem, item == currentItem {
  41. self.seek(to: .zero)
  42. self.playerToolBar?.showToolBar(metadata: metadata, detailView: nil)
  43. NCKTVHTTPCache.shared.saveCache(metadata: metadata)
  44. }
  45. }
  46. currentItem?.asset.loadValuesAsynchronously(forKeys: ["duration", "playable"], completionHandler: {
  47. if let duration: CMTime = (self.currentItem?.asset.duration) {
  48. var error: NSError? = nil
  49. let status = self.currentItem?.asset.statusOfValue(forKey: "playable", error: &error)
  50. switch status {
  51. case .loaded:
  52. DispatchQueue.main.async {
  53. if let imageVideoContainer = imageVideoContainer {
  54. self.imageVideoContainer = imageVideoContainer
  55. self.videoLayer = AVPlayerLayer(player: self)
  56. self.videoLayer!.frame = imageVideoContainer.bounds
  57. self.videoLayer!.videoGravity = .resizeAspect
  58. imageVideoContainer.layer.addSublayer(self.videoLayer!)
  59. imageVideoContainer.playerLayer = self.videoLayer
  60. }
  61. self.durationSeconds = CMTimeGetSeconds(duration)
  62. NCManageDatabase.shared.addVideoTime(metadata: metadata, time: nil, durationSeconds: self.durationSeconds)
  63. // NO Live Photo, seek to datamebase time
  64. if !metadata.livePhoto, let time = NCManageDatabase.shared.getVideoTime(metadata: metadata) {
  65. self.seek(to: time)
  66. }
  67. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  68. playerToolBar?.setBarPlayer(player: self)
  69. }
  70. }
  71. break
  72. case .failed:
  73. DispatchQueue.main.async {
  74. NCContentPresenter.shared.messageNotification("_error_", description: "_error_something_wrong_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.info, errorCode: NCGlobal.shared.errorGeneric, forced: false)
  75. }
  76. break
  77. case .cancelled:
  78. DispatchQueue.main.async {
  79. //do something, show alert, put a placeholder image etc.
  80. }
  81. break
  82. default:
  83. break
  84. }
  85. }
  86. })
  87. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
  88. }
  89. deinit {
  90. print("deinit NCPlayer")
  91. }
  92. //MARK: - NotificationCenter
  93. @objc func applicationDidEnterBackground(_ notification: NSNotification) {
  94. if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  95. self.pause()
  96. }
  97. }
  98. //MARK: -
  99. func deleteLocalFile() {
  100. guard let metadata = self.metadata else { return }
  101. NCManageDatabase.shared.deleteVideoTime(metadata: metadata)
  102. NCManageDatabase.shared.deleteLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  103. NCUtilityFileSystem.shared.deleteFile(filePath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId))
  104. }
  105. func videoPlay() {
  106. play()
  107. }
  108. func videoPause() {
  109. guard let metadata = self.metadata else { return }
  110. pause()
  111. NCManageDatabase.shared.addVideoTime(metadata: metadata, time: currentTime(), durationSeconds: nil)
  112. }
  113. func videoSeek(time: CMTime) {
  114. guard let metadata = self.metadata else { return }
  115. seek(to: time)
  116. NCManageDatabase.shared.addVideoTime(metadata: metadata, time: time, durationSeconds: nil)
  117. }
  118. func videoRemoved() {
  119. videoPause()
  120. self.videoLayer?.removeFromSuperlayer()
  121. }
  122. func getVideoCurrentSeconds() -> Float64 {
  123. return CMTimeGetSeconds(currentTime())
  124. }
  125. func getVideoDurationSeconds() -> Float64 {
  126. return self.durationSeconds
  127. }
  128. }