NCPlayerToolBar.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. //
  2. // NCPlayerToolBar.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 CoreMedia
  26. import UIKit
  27. import AVKit
  28. import MediaPlayer
  29. import MobileVLCKit
  30. import FloatingPanel
  31. class NCPlayerToolBar: UIView {
  32. @IBOutlet weak var utilityView: UIView!
  33. @IBOutlet weak var fullscreenButton: UIButton!
  34. @IBOutlet weak var subtitleButton: UIButton!
  35. @IBOutlet weak var audioButton: UIButton!
  36. @IBOutlet weak var playerButtonView: UIStackView!
  37. @IBOutlet weak var backButton: UIButton!
  38. @IBOutlet weak var playButton: UIButton!
  39. @IBOutlet weak var forwardButton: UIButton!
  40. @IBOutlet weak var playbackSliderView: UIView!
  41. @IBOutlet weak var playbackSlider: UISlider!
  42. @IBOutlet weak var labelLeftTime: UILabel!
  43. @IBOutlet weak var labelCurrentTime: UILabel!
  44. enum sliderEventType {
  45. case began
  46. case ended
  47. case moved
  48. }
  49. var playbackSliderEvent: sliderEventType = .ended
  50. var isFullscreen: Bool = false
  51. private var ncplayer: NCPlayer?
  52. private var metadata: tableMetadata?
  53. private let audioSession = AVAudioSession.sharedInstance()
  54. private var pointSize: CGFloat = 0
  55. private weak var viewerMediaPage: NCViewerMediaPage?
  56. // MARK: - View Life Cycle
  57. override func awakeFromNib() {
  58. super.awakeFromNib()
  59. self.backgroundColor = UIColor.black.withAlphaComponent(0.1)
  60. fullscreenButton.setImage(NCUtility.shared.loadImage(named: "arrow.up.left.and.arrow.down.right", color: .white), for: .normal)
  61. subtitleButton.setImage(NCUtility.shared.loadImage(named: "captions.bubble", color: .white), for: .normal)
  62. subtitleButton.isEnabled = false
  63. audioButton.setImage(NCUtility.shared.loadImage(named: "speaker.zzz", color: .white), for: .normal)
  64. audioButton.isEnabled = false
  65. if UIDevice.current.userInterfaceIdiom == .pad {
  66. pointSize = 60
  67. } else {
  68. pointSize = 50
  69. }
  70. playerButtonView.spacing = pointSize
  71. playerButtonView.isHidden = true
  72. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.10", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: pointSize)), for: .normal)
  73. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: pointSize)), for: .normal)
  74. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.10", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: pointSize)), for: .normal)
  75. playbackSlider.setThumbImage(UIImage(systemName: "circle.fill", withConfiguration: UIImage.SymbolConfiguration(pointSize: 15)), for: .normal)
  76. playbackSlider.value = 0
  77. playbackSlider.tintColor = .white
  78. playbackSlider.addTarget(self, action: #selector(playbackValChanged(slider:event:)), for: .valueChanged)
  79. utilityView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tap(gestureRecognizer:))))
  80. playbackSliderView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tap(gestureRecognizer:))))
  81. playerButtonView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tap(gestureRecognizer:))))
  82. labelCurrentTime.textColor = .white
  83. labelLeftTime.textColor = .white
  84. // Normally hide
  85. self.alpha = 0
  86. self.isHidden = true
  87. }
  88. required init?(coder aDecoder: NSCoder) {
  89. super.init(coder: aDecoder)
  90. }
  91. deinit {
  92. print("deinit NCPlayerToolBar")
  93. }
  94. // MARK: -
  95. func setBarPlayer(position: Float, ncplayer: NCPlayer? = nil, metadata: tableMetadata? = nil, viewerMediaPage: NCViewerMediaPage? = nil) {
  96. if let ncplayer = ncplayer {
  97. self.ncplayer = ncplayer
  98. }
  99. if let metadata = metadata {
  100. self.metadata = metadata
  101. }
  102. if let viewerMediaPage = viewerMediaPage {
  103. self.viewerMediaPage = viewerMediaPage
  104. }
  105. playerButtonView.isHidden = true
  106. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: pointSize)), for: .normal)
  107. playbackSlider.value = position
  108. labelCurrentTime.text = "--:--"
  109. labelLeftTime.text = "--:--"
  110. if viewerMediaScreenMode == .normal {
  111. show()
  112. } else {
  113. hide()
  114. }
  115. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = position
  116. }
  117. public func update() {
  118. guard let ncplayer = self.ncplayer,
  119. let length = ncplayer.player.media?.length.intValue
  120. else { return }
  121. let position = ncplayer.player.position
  122. let positionInSecond = position * Float(length / 1000)
  123. // SLIDER & TIME
  124. if playbackSliderEvent == .ended {
  125. playbackSlider.value = position
  126. }
  127. labelCurrentTime.text = ncplayer.player.time.stringValue
  128. labelLeftTime.text = ncplayer.player.remainingTime?.stringValue
  129. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyPlaybackDuration] = length / 1000
  130. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = positionInSecond
  131. }
  132. public func updateTopToolBar(videoSubTitlesIndexes: [Any], audioTrackIndexes: [Any]) {
  133. self.subtitleButton.isEnabled = !videoSubTitlesIndexes.isEmpty
  134. self.audioButton.isEnabled = !audioTrackIndexes.isEmpty
  135. }
  136. // MARK: -
  137. public func show() {
  138. UIView.animate(withDuration: 0.5, animations: {
  139. self.alpha = 1
  140. }, completion: { (_: Bool) in
  141. self.isHidden = false
  142. })
  143. }
  144. func hide() {
  145. UIView.animate(withDuration: 0.5, animations: {
  146. self.alpha = 0
  147. }, completion: { (_: Bool) in
  148. self.isHidden = true
  149. })
  150. }
  151. func playButtonPause() {
  152. playButton.setImage(NCUtility.shared.loadImage(named: "pause.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: pointSize)), for: .normal)
  153. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 1
  154. }
  155. func playButtonPlay() {
  156. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: pointSize)), for: .normal)
  157. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0
  158. }
  159. // MARK: - Event / Gesture
  160. @objc func playbackValChanged(slider: UISlider, event: UIEvent) {
  161. guard let touchEvent = event.allTouches?.first,
  162. let ncplayer = ncplayer
  163. else { return }
  164. let newPosition = playbackSlider.value
  165. switch touchEvent.phase {
  166. case .began:
  167. viewerMediaPage?.timerAutoHide?.invalidate()
  168. playbackSliderEvent = .began
  169. case .moved:
  170. ncplayer.playerPosition(newPosition)
  171. playbackSliderEvent = .moved
  172. case .ended:
  173. ncplayer.playerPosition(newPosition)
  174. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  175. self.playbackSliderEvent = .ended
  176. self.viewerMediaPage?.startTimerAutoHide()
  177. }
  178. default:
  179. break
  180. }
  181. }
  182. // MARK: - Action
  183. @objc func tap(gestureRecognizer: UITapGestureRecognizer) { }
  184. @IBAction func tapFullscreen(_ sender: Any) {
  185. isFullscreen = !isFullscreen
  186. if isFullscreen {
  187. fullscreenButton.setImage(NCUtility.shared.loadImage(named: "arrow.down.right.and.arrow.up.left", color: .white), for: .normal)
  188. } else {
  189. fullscreenButton.setImage(NCUtility.shared.loadImage(named: "arrow.up.left.and.arrow.down.right", color: .white), for: .normal)
  190. }
  191. viewerMediaPage?.changeScreenMode(mode: viewerMediaScreenMode)
  192. }
  193. @IBAction func tapSubTitle(_ sender: Any) {
  194. guard let player = ncplayer?.player else { return }
  195. let spuTracks = player.videoSubTitlesNames
  196. let spuTrackIndexes = player.videoSubTitlesIndexes
  197. let count = spuTracks.count
  198. if count > 1 {
  199. toggleMenuSubTitle(spuTracks: spuTracks, spuTrackIndexes: spuTrackIndexes)
  200. }
  201. }
  202. @IBAction func tapAudio(_ sender: Any) {
  203. guard let player = ncplayer?.player else { return }
  204. let audioTracks = player.audioTrackNames
  205. let audioTrackIndexes = player.audioTrackIndexes
  206. let count = audioTracks.count
  207. if count > 1 {
  208. toggleMenuAudio(audioTracks: audioTracks, audioTrackIndexes: audioTrackIndexes)
  209. }
  210. }
  211. @IBAction func tapPlayerPause(_ sender: Any) {
  212. guard let ncplayer = ncplayer else { return }
  213. if ncplayer.isPlay() {
  214. ncplayer.playerPause()
  215. } else {
  216. ncplayer.playerPlay()
  217. }
  218. self.viewerMediaPage?.startTimerAutoHide()
  219. }
  220. @IBAction func tapForward(_ sender: Any) {
  221. guard let ncplayer = ncplayer else { return }
  222. ncplayer.jumpForward(10)
  223. self.viewerMediaPage?.startTimerAutoHide()
  224. }
  225. @IBAction func tapBack(_ sender: Any) {
  226. guard let ncplayer = ncplayer else { return }
  227. ncplayer.jumpBackward(10)
  228. self.viewerMediaPage?.startTimerAutoHide()
  229. }
  230. }
  231. extension NCPlayerToolBar {
  232. func toggleMenuSubTitle(spuTracks: [Any], spuTrackIndexes: [Any]) {
  233. var actions = [NCMenuAction]()
  234. var subTitleIndex: Int?
  235. if let data = NCManageDatabase.shared.getVideo(metadata: metadata), let idx = data.currentVideoSubTitleIndex {
  236. subTitleIndex = idx
  237. } else if let idx = ncplayer?.player.currentVideoSubTitleIndex {
  238. subTitleIndex = Int(idx)
  239. }
  240. for index in 0...spuTracks.count - 1 {
  241. guard let title = spuTracks[index] as? String, let idx = spuTrackIndexes[index] as? Int32, let metadata = self.metadata else { return }
  242. actions.append(
  243. NCMenuAction(
  244. title: title,
  245. icon: UIImage(),
  246. onTitle: title,
  247. onIcon: UIImage(),
  248. selected: (subTitleIndex ?? -9999) == idx,
  249. on: (subTitleIndex ?? -9999) == idx,
  250. action: { _ in
  251. self.ncplayer?.player.currentVideoSubTitleIndex = idx
  252. NCManageDatabase.shared.addVideo(metadata: metadata, currentVideoSubTitleIndex: Int(idx))
  253. }
  254. )
  255. )
  256. }
  257. viewerMediaPage?.presentMenu(with: actions, menuColor: UIColor(hexString: "#1C1C1EFF"), textColor: .white)
  258. }
  259. func toggleMenuAudio(audioTracks: [Any], audioTrackIndexes: [Any]) {
  260. var actions = [NCMenuAction]()
  261. var audioIndex: Int?
  262. if let data = NCManageDatabase.shared.getVideo(metadata: metadata), let idx = data.currentAudioTrackIndex {
  263. audioIndex = idx
  264. } else if let idx = ncplayer?.player.currentAudioTrackIndex {
  265. audioIndex = Int(idx)
  266. }
  267. for index in 0...audioTracks.count - 1 {
  268. guard let title = audioTracks[index] as? String, let idx = audioTrackIndexes[index] as? Int32, let metadata = self.metadata else { return }
  269. actions.append(
  270. NCMenuAction(
  271. title: title,
  272. icon: UIImage(),
  273. onTitle: title,
  274. onIcon: UIImage(),
  275. selected: (audioIndex ?? -9999) == idx,
  276. on: (audioIndex ?? -9999) == idx,
  277. action: { _ in
  278. self.ncplayer?.player.currentAudioTrackIndex = idx
  279. NCManageDatabase.shared.addVideo(metadata: metadata, currentAudioTrackIndex: Int(idx))
  280. }
  281. )
  282. )
  283. }
  284. viewerMediaPage?.presentMenu(with: actions, menuColor: UIColor(hexString: "#1C1C1EFF"), textColor: .white)
  285. }
  286. }