NCPlayerToolBar.swift 13 KB

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