NCPlayer.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 NextcloudKit
  25. import UIKit
  26. import MobileVLCKit
  27. class NCPlayer: NSObject {
  28. internal let appDelegate = UIApplication.shared.delegate as! AppDelegate
  29. internal var url: URL?
  30. internal var player = VLCMediaPlayer()
  31. internal var metadata: tableMetadata
  32. internal var singleTapGestureRecognizer: UITapGestureRecognizer!
  33. internal var width: Int?
  34. internal var height: Int?
  35. internal var length: Int?
  36. internal let fileNamePreviewLocalPath: String
  37. internal weak var playerToolBar: NCPlayerToolBar?
  38. internal weak var viewerMediaPage: NCViewerMediaPage?
  39. weak var imageVideoContainer: imageVideoContainerView?
  40. internal var counterSeconds: Double = 0
  41. // MARK: - View Life Cycle
  42. init(imageVideoContainer: imageVideoContainerView, playerToolBar: NCPlayerToolBar?, metadata: tableMetadata, viewerMediaPage: NCViewerMediaPage?) {
  43. self.imageVideoContainer = imageVideoContainer
  44. self.playerToolBar = playerToolBar
  45. self.metadata = metadata
  46. self.viewerMediaPage = viewerMediaPage
  47. fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
  48. super.init()
  49. }
  50. deinit {
  51. print("deinit NCPlayer with ocId \(metadata.ocId)")
  52. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
  53. }
  54. func openAVPlayer(url: URL, autoplay: Bool = false) {
  55. let userAgent = CCUtility.getUserAgent()!
  56. var positionSliderToolBar: Float = 0
  57. self.url = url
  58. self.singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSingleTapWith(gestureRecognizer:)))
  59. print("Play URL: \(url)")
  60. player.media = VLCMedia(url: url)
  61. player.delegate = self
  62. // player?.media?.addOption("--network-caching=500")
  63. player.media?.addOption(":http-user-agent=\(userAgent)")
  64. if let result = NCManageDatabase.shared.getVideo(metadata: metadata), let position = result.position {
  65. positionSliderToolBar = position
  66. player.position = positionSliderToolBar
  67. }
  68. player.drawable = imageVideoContainer
  69. if let view = player.drawable as? UIView {
  70. view.isUserInteractionEnabled = true
  71. view.addGestureRecognizer(singleTapGestureRecognizer)
  72. }
  73. playerToolBar?.setBarPlayer(ncplayer: self, position: positionSliderToolBar, metadata: metadata, viewerMediaPage: viewerMediaPage)
  74. if autoplay {
  75. player.play()
  76. }
  77. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
  78. }
  79. // MARK: - UIGestureRecognizerDelegate
  80. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  81. viewerMediaPage?.didSingleTapWith(gestureRecognizer: gestureRecognizer)
  82. }
  83. // MARK: - NotificationCenter
  84. @objc func applicationDidEnterBackground(_ notification: NSNotification) {
  85. if metadata.isVideo {
  86. playerStop()
  87. }
  88. }
  89. // MARK: -
  90. func isPlay() -> Bool {
  91. return player.isPlaying
  92. }
  93. func playerPlay() {
  94. playerToolBar?.playbackSliderEvent = .began
  95. player.play()
  96. playerToolBar?.playButtonPause()
  97. if let result = NCManageDatabase.shared.getVideo(metadata: metadata), let position = result.position {
  98. player.position = position
  99. playerToolBar?.playbackSliderEvent = .moved
  100. }
  101. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  102. self.playerToolBar?.playbackSliderEvent = .ended
  103. }
  104. }
  105. @objc func playerStop() {
  106. savePosition()
  107. player.stop()
  108. playerToolBar?.playButtonPlay()
  109. }
  110. @objc func playerPause() {
  111. savePosition()
  112. player.pause()
  113. playerToolBar?.playButtonPlay()
  114. }
  115. func playerPosition(_ position: Float) {
  116. NCManageDatabase.shared.addVideo(metadata: metadata, position: position)
  117. player.position = position
  118. }
  119. func savePosition() {
  120. guard metadata.isVideo, isPlay() else { return }
  121. NCManageDatabase.shared.addVideo(metadata: metadata, position: player.position)
  122. }
  123. func jumpForward(_ seconds: Int32) {
  124. player.jumpForward(seconds)
  125. }
  126. func jumpBackward(_ seconds: Int32) {
  127. player.jumpBackward(seconds)
  128. }
  129. }
  130. extension NCPlayer: VLCMediaPlayerDelegate {
  131. func mediaPlayerStateChanged(_ aNotification: Notification) {
  132. switch player.state {
  133. case .stopped:
  134. print("Played mode: STOPPED")
  135. break
  136. case .opening:
  137. print("Played mode: OPENING")
  138. break
  139. case .buffering:
  140. print("Played mode: BUFFERING")
  141. break
  142. case .ended:
  143. if let url = self.url {
  144. NCManageDatabase.shared.addVideo(metadata: metadata, position: 0)
  145. self.openAVPlayer(url: url)
  146. }
  147. print("Played mode: ENDED")
  148. break
  149. case .error:
  150. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_error_something_wrong_")
  151. NCContentPresenter.shared.showError(error: error, priority: .max)
  152. playerToolBar?.removeFromSuperview()
  153. print("Played mode: ERROR")
  154. break
  155. case .playing:
  156. let size = player.videoSize
  157. if let mediaLength = player.media?.length.intValue {
  158. self.length = Int(mediaLength)
  159. }
  160. self.width = Int(size.width)
  161. self.height = Int(size.height)
  162. playerToolBar?.updateTopToolBar(videoSubTitlesIndexes: player.videoSubTitlesIndexes, audioTrackIndexes: player.audioTrackIndexes)
  163. NCManageDatabase.shared.addVideo(metadata: metadata, width: self.width, height: self.height, length: self.length)
  164. print("Played mode: PLAYING")
  165. break
  166. case .paused:
  167. print("Played mode: PAUSED")
  168. break
  169. default: break
  170. }
  171. }
  172. func mediaPlayerTimeChanged(_ aNotification: Notification) {
  173. playerToolBar?.update()
  174. }
  175. func mediaPlayerTitleChanged(_ aNotification: Notification) {
  176. // Handle other states...
  177. }
  178. func mediaPlayerChapterChanged(_ aNotification: Notification) {
  179. // Handle other states...
  180. }
  181. func mediaPlayerLoudnessChanged(_ aNotification: Notification) {
  182. // Handle other states...
  183. }
  184. func mediaPlayerSnapshot(_ aNotification: Notification) {
  185. print("Snapshot saved")
  186. }
  187. func mediaPlayerStartedRecording(_ player: VLCMediaPlayer) {
  188. // Handle other states...
  189. }
  190. func mediaPlayer(_ player: VLCMediaPlayer, recordingStoppedAtPath path: String) {
  191. // Handle other states...
  192. }
  193. }
  194. extension NCPlayer: VLCMediaThumbnailerDelegate {
  195. func mediaThumbnailerDidTimeOut(_ mediaThumbnailer: VLCMediaThumbnailer) { }
  196. func mediaThumbnailer(_ mediaThumbnailer: VLCMediaThumbnailer, didFinishThumbnail thumbnail: CGImage) { }
  197. }