NCPlayerToolBar.swift 12 KB

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