NCPlayerToolBar.swift 11 KB

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