NCPlayer.swift 8.3 KB

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