NCPlayerToolBar.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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(position: Float, ncplayer: NCPlayer? = nil, metadata: tableMetadata? = nil, viewerMediaPage: NCViewerMediaPage? = nil) {
  95. if let ncplayer = ncplayer {
  96. self.ncplayer = ncplayer
  97. }
  98. if let metadata = metadata {
  99. self.metadata = metadata
  100. }
  101. if let viewerMediaPage = viewerMediaPage {
  102. self.viewerMediaPage = viewerMediaPage
  103. }
  104. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  105. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0
  106. playbackSlider.value = position
  107. labelCurrentTime.text = "--:--"
  108. labelLeftTime.text = "--:--"
  109. if viewerMediaScreenMode == .normal {
  110. show()
  111. } else {
  112. hide()
  113. }
  114. }
  115. public func update() {
  116. guard let ncplayer = self.ncplayer,
  117. let length = ncplayer.player.media?.length.intValue
  118. else { return }
  119. let position = ncplayer.player.position
  120. let positionInSecond = position * Float(length / 1000)
  121. // SLIDER & TIME
  122. if playbackSliderEvent == .ended {
  123. playbackSlider.value = position
  124. }
  125. labelCurrentTime.text = ncplayer.player.time.stringValue
  126. labelLeftTime.text = ncplayer.player.remainingTime?.stringValue
  127. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyPlaybackDuration] = length / 1000
  128. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = positionInSecond
  129. }
  130. public func updateTopToolBar(videoSubTitlesIndexes: [Any], audioTrackIndexes: [Any]) {
  131. self.subtitleButton.isEnabled = !videoSubTitlesIndexes.isEmpty
  132. self.audioButton.isEnabled = !audioTrackIndexes.isEmpty
  133. }
  134. // MARK: -
  135. public func show() {
  136. UIView.animate(withDuration: 0.3, animations: {
  137. self.alpha = 1
  138. }, completion: { (_: Bool) in
  139. self.isHidden = false
  140. })
  141. }
  142. func hide() {
  143. UIView.animate(withDuration: 0.3, animations: {
  144. self.alpha = 0
  145. }, completion: { (_: Bool) in
  146. self.isHidden = true
  147. })
  148. }
  149. func playButtonPause() {
  150. playButton.setImage(NCUtility.shared.loadImage(named: "pause.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  151. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 1
  152. }
  153. func playButtonPlay() {
  154. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  155. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0
  156. }
  157. // MARK: - Event / Gesture
  158. @objc func playbackValChanged(slider: UISlider, event: UIEvent) {
  159. guard let touchEvent = event.allTouches?.first,
  160. let ncplayer = ncplayer
  161. else { return }
  162. let newPosition = playbackSlider.value
  163. switch touchEvent.phase {
  164. case .began:
  165. viewerMediaPage?.timerAutoHide?.invalidate()
  166. playbackSliderEvent = .began
  167. case .moved:
  168. ncplayer.playerPosition(newPosition)
  169. playbackSliderEvent = .moved
  170. case .ended:
  171. ncplayer.playerPosition(newPosition)
  172. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  173. self.playbackSliderEvent = .ended
  174. self.viewerMediaPage?.startTimerAutoHide()
  175. }
  176. default:
  177. break
  178. }
  179. }
  180. // MARK: - Action
  181. @objc func tapTopToolBarWith(gestureRecognizer: UITapGestureRecognizer) { }
  182. @objc func tapToolBarWith(gestureRecognizer: UITapGestureRecognizer) { }
  183. @IBAction func tapPlayerPause(_ sender: Any) {
  184. guard let ncplayer = ncplayer else { return }
  185. if ncplayer.isPlay() {
  186. ncplayer.playerPause()
  187. } else {
  188. ncplayer.playerPlay()
  189. }
  190. self.viewerMediaPage?.startTimerAutoHide()
  191. }
  192. @IBAction func tapSubTitle(_ sender: Any) {
  193. guard let player = ncplayer?.player else { return }
  194. let spuTracks = player.videoSubTitlesNames
  195. let spuTrackIndexes = player.videoSubTitlesIndexes
  196. let count = spuTracks.count
  197. if count > 1 {
  198. toggleMenuSubTitle(spuTracks: spuTracks, spuTrackIndexes: spuTrackIndexes)
  199. }
  200. }
  201. @IBAction func tapAudio(_ sender: Any) {
  202. guard let player = ncplayer?.player else { return }
  203. let audioTracks = player.audioTrackNames
  204. let audioTrackIndexes = player.audioTrackIndexes
  205. let count = audioTracks.count
  206. if count > 1 {
  207. toggleMenuAudio(audioTracks: audioTracks, audioTrackIndexes: audioTrackIndexes)
  208. }
  209. }
  210. @IBAction func tapForward(_ sender: Any) {
  211. guard let ncplayer = ncplayer else { return }
  212. ncplayer.jumpForward(10)
  213. self.viewerMediaPage?.startTimerAutoHide()
  214. }
  215. @IBAction func tapBack(_ sender: Any) {
  216. guard let ncplayer = ncplayer else { return }
  217. ncplayer.jumpBackward(10)
  218. self.viewerMediaPage?.startTimerAutoHide()
  219. }
  220. }
  221. extension NCPlayerToolBar {
  222. func toggleMenuSubTitle(spuTracks: [Any], spuTrackIndexes: [Any]) {
  223. var actions = [NCMenuAction]()
  224. if self.subTitleIndex == nil, let idx = ncplayer?.player.currentVideoSubTitleIndex {
  225. self.subTitleIndex = idx
  226. }
  227. for index in 0...spuTracks.count - 1 {
  228. guard let title = spuTracks[index] as? String, let idx = spuTrackIndexes[index] as? Int32 else { return }
  229. actions.append(
  230. NCMenuAction(
  231. title: title,
  232. icon: UIImage(),
  233. onTitle: title,
  234. onIcon: UIImage(),
  235. selected: (self.subTitleIndex ?? -9999) == idx,
  236. on: (self.subTitleIndex ?? -9999) == idx,
  237. action: { _ in
  238. self.ncplayer?.player.currentVideoSubTitleIndex = idx
  239. self.subTitleIndex = idx
  240. }
  241. )
  242. )
  243. }
  244. viewerMediaPage?.presentMenu(with: actions, menuColor: UIColor(hexString: "#1C1C1EFF"), textColor: .white)
  245. }
  246. func toggleMenuAudio(audioTracks: [Any], audioTrackIndexes: [Any]) {
  247. var actions = [NCMenuAction]()
  248. if self.audioIndex == nil, let idx = ncplayer?.player.currentAudioTrackIndex {
  249. self.audioIndex = idx
  250. }
  251. for index in 0...audioTracks.count - 1 {
  252. guard let title = audioTracks[index] as? String, let idx = audioTrackIndexes[index] as? Int32 else { return }
  253. actions.append(
  254. NCMenuAction(
  255. title: title,
  256. icon: UIImage(),
  257. onTitle: title,
  258. onIcon: UIImage(),
  259. selected: (self.audioIndex ?? -9999) == idx,
  260. on: (self.audioIndex ?? -9999) == idx,
  261. action: { _ in
  262. self.ncplayer?.player.currentAudioTrackIndex = idx
  263. self.audioIndex = idx
  264. }
  265. )
  266. )
  267. }
  268. viewerMediaPage?.presentMenu(with: actions, menuColor: UIColor(hexString: "#1C1C1EFF"), textColor: .white)
  269. }
  270. }