NCPlayerToolBar.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. // @IBOutlet weak var brightnessView: UIView!
  43. // @IBOutlet weak var volumeView: UIView!
  44. enum sliderEventType {
  45. case began
  46. case ended
  47. case moved
  48. }
  49. var playbackSliderEvent: sliderEventType = .ended
  50. private var ncplayer: NCPlayer?
  51. private var metadata: tableMetadata?
  52. private let audioSession = AVAudioSession.sharedInstance()
  53. private var subTitleIndex: Int32?
  54. private var audioIndex: Int32?
  55. private weak var viewerMediaPage: NCViewerMediaPage?
  56. // MARK: - View Life Cycle
  57. override func awakeFromNib() {
  58. super.awakeFromNib()
  59. let blurEffectTopToolBarView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
  60. blurEffectTopToolBarView.frame = playerTopToolBarView.bounds
  61. blurEffectTopToolBarView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  62. playerTopToolBarView.insertSubview(blurEffectTopToolBarView, at: 0)
  63. playerTopToolBarView.layer.cornerRadius = 10
  64. playerTopToolBarView.layer.masksToBounds = true
  65. playerTopToolBarView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapTopToolBarWith(gestureRecognizer:))))
  66. let blurEffectToolBarView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
  67. blurEffectToolBarView.frame = playerToolBarView.bounds
  68. blurEffectToolBarView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  69. playerToolBarView.insertSubview(blurEffectToolBarView, at: 0)
  70. playerToolBarView.layer.cornerRadius = 10
  71. playerToolBarView.layer.masksToBounds = true
  72. playerToolBarView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapToolBarWith(gestureRecognizer:))))
  73. playbackSlider.value = 0
  74. playbackSlider.tintColor = .lightGray
  75. playbackSlider.addTarget(self, action: #selector(playbackValChanged(slider:event:)), for: .valueChanged)
  76. labelCurrentTime.textColor = .white
  77. labelLeftTime.textColor = .white
  78. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  79. subtitleButton.setImage(NCUtility.shared.loadImage(named: "captions.bubble", color: .white), for: .normal)
  80. subtitleButton.isEnabled = false
  81. audioButton.setImage(NCUtility.shared.loadImage(named: "speaker.zzz", color: .white), for: .normal)
  82. audioButton.isEnabled = false
  83. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.10", color: .white), for: .normal)
  84. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.10", color: .white), for: .normal)
  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. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  107. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0
  108. playbackSlider.value = position
  109. labelCurrentTime.text = "--:--"
  110. labelLeftTime.text = "--:--"
  111. if viewerMediaScreenMode == .normal {
  112. show()
  113. } else {
  114. hide()
  115. }
  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.3, animations: {
  139. self.alpha = 1
  140. }, completion: { (_: Bool) in
  141. self.isHidden = false
  142. })
  143. }
  144. func hide() {
  145. UIView.animate(withDuration: 0.3, 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: 30)), 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: 30)), 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 tapTopToolBarWith(gestureRecognizer: UITapGestureRecognizer) { }
  184. @objc func tapToolBarWith(gestureRecognizer: UITapGestureRecognizer) { }
  185. @IBAction func tapPlayerPause(_ sender: Any) {
  186. guard let ncplayer = ncplayer else { return }
  187. if ncplayer.isPlay() {
  188. ncplayer.playerPause()
  189. } else {
  190. ncplayer.playerPlay()
  191. }
  192. self.viewerMediaPage?.startTimerAutoHide()
  193. }
  194. @IBAction func tapSubTitle(_ sender: Any) {
  195. guard let player = ncplayer?.player else { return }
  196. let spuTracks = player.videoSubTitlesNames
  197. let spuTrackIndexes = player.videoSubTitlesIndexes
  198. let count = spuTracks.count
  199. if count > 1 {
  200. toggleMenuSubTitle(spuTracks: spuTracks, spuTrackIndexes: spuTrackIndexes)
  201. }
  202. }
  203. @IBAction func tapAudio(_ sender: Any) {
  204. guard let player = ncplayer?.player else { return }
  205. let audioTracks = player.audioTrackNames
  206. let audioTrackIndexes = player.audioTrackIndexes
  207. let count = audioTracks.count
  208. if count > 1 {
  209. toggleMenuAudio(audioTracks: audioTracks, audioTrackIndexes: audioTrackIndexes)
  210. }
  211. }
  212. @IBAction func tapForward(_ sender: Any) {
  213. guard let ncplayer = ncplayer else { return }
  214. ncplayer.jumpForward(10)
  215. self.viewerMediaPage?.startTimerAutoHide()
  216. }
  217. @IBAction func tapBack(_ sender: Any) {
  218. guard let ncplayer = ncplayer else { return }
  219. ncplayer.jumpBackward(10)
  220. self.viewerMediaPage?.startTimerAutoHide()
  221. }
  222. }
  223. extension NCPlayerToolBar {
  224. func toggleMenuSubTitle(spuTracks: [Any], spuTrackIndexes: [Any]) {
  225. var actions = [NCMenuAction]()
  226. if self.subTitleIndex == nil, let idx = ncplayer?.player.currentVideoSubTitleIndex {
  227. self.subTitleIndex = idx
  228. }
  229. for index in 0...spuTracks.count - 1 {
  230. guard let title = spuTracks[index] as? String, let idx = spuTrackIndexes[index] as? Int32 else { return }
  231. actions.append(
  232. NCMenuAction(
  233. title: title,
  234. icon: UIImage(),
  235. onTitle: title,
  236. onIcon: UIImage(),
  237. selected: (self.subTitleIndex ?? -9999) == idx,
  238. on: (self.subTitleIndex ?? -9999) == idx,
  239. action: { _ in
  240. self.ncplayer?.player.currentVideoSubTitleIndex = idx
  241. self.subTitleIndex = idx
  242. }
  243. )
  244. )
  245. }
  246. viewerMediaPage?.presentMenu(with: actions, menuColor: UIColor(hexString: "#1C1C1EFF"), textColor: .white)
  247. }
  248. func toggleMenuAudio(audioTracks: [Any], audioTrackIndexes: [Any]) {
  249. var actions = [NCMenuAction]()
  250. if self.audioIndex == nil, let idx = ncplayer?.player.currentAudioTrackIndex {
  251. self.audioIndex = idx
  252. }
  253. for index in 0...audioTracks.count - 1 {
  254. guard let title = audioTracks[index] as? String, let idx = audioTrackIndexes[index] as? Int32 else { return }
  255. actions.append(
  256. NCMenuAction(
  257. title: title,
  258. icon: UIImage(),
  259. onTitle: title,
  260. onIcon: UIImage(),
  261. selected: (self.audioIndex ?? -9999) == idx,
  262. on: (self.audioIndex ?? -9999) == idx,
  263. action: { _ in
  264. self.ncplayer?.player.currentAudioTrackIndex = idx
  265. self.audioIndex = idx
  266. }
  267. )
  268. )
  269. }
  270. viewerMediaPage?.presentMenu(with: actions, menuColor: UIColor(hexString: "#1C1C1EFF"), textColor: .white)
  271. }
  272. }