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