NCPlayerToolBar.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. class NCPlayerToolBar: UIView {
  31. @IBOutlet weak var playerTopToolBarView: UIStackView!
  32. @IBOutlet weak var playerToolBarView: UIView!
  33. @IBOutlet weak var muteButton: UIButton!
  34. @IBOutlet weak var playButton: UIButton!
  35. @IBOutlet weak var forwardButton: UIButton!
  36. @IBOutlet weak var backButton: UIButton!
  37. @IBOutlet weak var playbackSlider: UISlider!
  38. @IBOutlet weak var labelLeftTime: UILabel!
  39. @IBOutlet weak var labelCurrentTime: UILabel!
  40. enum sliderEventType {
  41. case began
  42. case ended
  43. case moved
  44. }
  45. var playbackSliderEvent: sliderEventType = .ended
  46. private var ncplayer: NCPlayer?
  47. private var metadata: tableMetadata?
  48. private var wasInPlay: Bool = false
  49. private var timerAutoHide: Timer?
  50. private var timerAutoHideSeconds: Double {
  51. get {
  52. if NCUtility.shared.isSimulator() { // for test
  53. return 3
  54. } else {
  55. return 5
  56. }
  57. }
  58. }
  59. // MARK: - View Life Cycle
  60. override func awakeFromNib() {
  61. super.awakeFromNib()
  62. let blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
  63. blurEffectView.frame = self.bounds
  64. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  65. playerToolBarView.insertSubview(blurEffectView, at: 0)
  66. playerTopToolBarView.layer.cornerRadius = 10
  67. playerTopToolBarView.layer.masksToBounds = true
  68. playerTopToolBarView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapTopToolBarWith(gestureRecognizer:))))
  69. let blurEffectTopToolBarView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
  70. blurEffectTopToolBarView.frame = playerTopToolBarView.bounds
  71. blurEffectTopToolBarView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  72. playerTopToolBarView.insertSubview(blurEffectTopToolBarView, at: 0)
  73. playerToolBarView.layer.cornerRadius = 10
  74. playerToolBarView.layer.masksToBounds = true
  75. playerToolBarView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapToolBarWith(gestureRecognizer:))))
  76. playbackSlider.value = 0
  77. playbackSlider.minimumValue = 0
  78. playbackSlider.maximumValue = 1
  79. playbackSlider.isContinuous = true
  80. playbackSlider.tintColor = .lightGray
  81. labelCurrentTime.textColor = .white
  82. labelLeftTime.textColor = .white
  83. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
  84. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  85. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.10", color: .white), for: .normal)
  86. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.10", color: .white), for: .normal)
  87. }
  88. required init?(coder aDecoder: NSCoder) {
  89. super.init(coder: aDecoder)
  90. }
  91. deinit {
  92. timerAutoHide?.invalidate()
  93. print("deinit NCPlayerToolBar")
  94. }
  95. // MARK: -
  96. func setBarPlayer(ncplayer: NCPlayer, position: Float, metadata: tableMetadata) {
  97. self.ncplayer = ncplayer
  98. self.metadata = metadata
  99. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  100. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0
  101. playbackSlider.value = position
  102. playbackSlider.addTarget(self, action: #selector(onSliderValChanged(slider:event:)), for: .valueChanged)
  103. labelCurrentTime.text = ncplayer.player?.time.stringValue
  104. labelLeftTime.text = ncplayer.player?.remainingTime?.stringValue
  105. if CCUtility.getAudioVolume() == 0 {
  106. ncplayer.setVolumeAudio(0)
  107. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .white), for: .normal)
  108. } else {
  109. ncplayer.setVolumeAudio(100)
  110. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
  111. }
  112. show()
  113. }
  114. public func update() {
  115. guard let ncplayer = self.ncplayer,
  116. let length = ncplayer.player?.media?.length.intValue,
  117. let position = ncplayer.player?.position
  118. else { return }
  119. let positionInSecond = position * Float(length / 1000)
  120. // SLIDER & TIME
  121. if playbackSliderEvent == .ended {
  122. playbackSlider.value = position
  123. }
  124. labelCurrentTime.text = ncplayer.player?.time.stringValue
  125. labelLeftTime.text = ncplayer.player?.remainingTime?.stringValue
  126. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyPlaybackDuration] = length / 1000
  127. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = positionInSecond
  128. }
  129. public func disableAllControl() {
  130. muteButton.isEnabled = false
  131. playButton.isEnabled = false
  132. forwardButton.isEnabled = false
  133. backButton.isEnabled = false
  134. playbackSlider.isEnabled = false
  135. }
  136. // MARK: -
  137. public func show() {
  138. if let ncplayer = ncplayer, ncplayer.isPlay() {
  139. startTimerAutoHide()
  140. }
  141. ncplayer?.viewerMediaPage?.changeScreenMode(mode: .normal, toggleToolbar: false)
  142. UIView.animate(withDuration: 0.3, animations: {
  143. self.alpha = 1
  144. }, completion: { (_: Bool) in
  145. self.isHidden = false
  146. })
  147. }
  148. private func startTimerAutoHide() {
  149. timerAutoHide?.invalidate()
  150. timerAutoHide = Timer.scheduledTimer(timeInterval: timerAutoHideSeconds, target: self, selector: #selector(hide), userInfo: nil, repeats: false)
  151. }
  152. @objc func hide() {
  153. ncplayer?.viewerMediaPage?.changeScreenMode(mode: .full, toggleToolbar: false)
  154. UIView.animate(withDuration: 0.3, animations: {
  155. self.alpha = 0
  156. }, completion: { (_: Bool) in
  157. self.isHidden = true
  158. })
  159. }
  160. func playButtonPause() {
  161. playButton.setImage(NCUtility.shared.loadImage(named: "pause.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  162. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 1
  163. }
  164. func playButtonPlay() {
  165. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  166. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0
  167. }
  168. // MARK: - Event / Gesture
  169. @objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
  170. guard let touchEvent = event.allTouches?.first,
  171. let ncplayer = ncplayer
  172. else { return }
  173. let newPosition = playbackSlider.value
  174. switch touchEvent.phase {
  175. case .began:
  176. playbackSliderEvent = .began
  177. case .moved:
  178. ncplayer.playerPosition(newPosition)
  179. playbackSliderEvent = .moved
  180. case .ended:
  181. ncplayer.playerPosition(newPosition)
  182. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  183. self.playbackSliderEvent = .ended
  184. }
  185. default:
  186. break
  187. }
  188. startTimerAutoHide()
  189. }
  190. // MARK: - Action
  191. @objc func tapTopToolBarWith(gestureRecognizer: UITapGestureRecognizer) { }
  192. @objc func tapToolBarWith(gestureRecognizer: UITapGestureRecognizer) { }
  193. @IBAction func tapPlayerPause(_ sender: Any) {
  194. guard let ncplayer = ncplayer else { return }
  195. if ncplayer.isPlay() {
  196. ncplayer.playerPause()
  197. timerAutoHide?.invalidate()
  198. } else {
  199. ncplayer.playerPlay()
  200. startTimerAutoHide()
  201. }
  202. }
  203. @IBAction func tapMute(_ sender: Any) {
  204. guard let ncplayer = ncplayer else { return }
  205. if CCUtility.getAudioVolume() > 0 {
  206. CCUtility.setAudioVolume(0)
  207. ncplayer.setVolumeAudio(0)
  208. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .white), for: .normal)
  209. } else {
  210. CCUtility.setAudioVolume(100)
  211. ncplayer.setVolumeAudio(100)
  212. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
  213. }
  214. startTimerAutoHide()
  215. }
  216. @IBAction func tapForward(_ sender: Any) {
  217. guard let ncplayer = ncplayer else { return }
  218. ncplayer.jumpForward(10)
  219. startTimerAutoHide()
  220. }
  221. @IBAction func tapBack(_ sender: Any) {
  222. guard let ncplayer = ncplayer else { return }
  223. ncplayer.jumpBackward(10)
  224. startTimerAutoHide()
  225. }
  226. }