NCPlayerToolBar.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. UIView.animate(withDuration: 0.3, animations: {
  142. self.alpha = 1
  143. }, completion: { (_: Bool) in
  144. self.isHidden = false
  145. })
  146. }
  147. private func startTimerAutoHide() {
  148. timerAutoHide?.invalidate()
  149. timerAutoHide = Timer.scheduledTimer(timeInterval: timerAutoHideSeconds, target: self, selector: #selector(hide), userInfo: nil, repeats: false)
  150. }
  151. @objc func hide() {
  152. UIView.animate(withDuration: 0.3, animations: {
  153. self.alpha = 0
  154. }, completion: { (_: Bool) in
  155. self.isHidden = true
  156. })
  157. }
  158. func playButtonPause() {
  159. playButton.setImage(NCUtility.shared.loadImage(named: "pause.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  160. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 1
  161. }
  162. func playButtonPlay() {
  163. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  164. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0
  165. }
  166. // MARK: - Event / Gesture
  167. @objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
  168. guard let touchEvent = event.allTouches?.first,
  169. let ncplayer = ncplayer
  170. else { return }
  171. let newPosition = playbackSlider.value
  172. switch touchEvent.phase {
  173. case .began:
  174. playbackSliderEvent = .began
  175. case .moved:
  176. ncplayer.playerPosition(newPosition)
  177. playbackSliderEvent = .moved
  178. case .ended:
  179. ncplayer.playerPosition(newPosition)
  180. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  181. self.playbackSliderEvent = .ended
  182. }
  183. default:
  184. break
  185. }
  186. startTimerAutoHide()
  187. }
  188. // MARK: - Action
  189. @objc func tapTopToolBarWith(gestureRecognizer: UITapGestureRecognizer) { }
  190. @objc func tapToolBarWith(gestureRecognizer: UITapGestureRecognizer) { }
  191. @IBAction func tapPlayerPause(_ sender: Any) {
  192. guard let ncplayer = ncplayer else { return }
  193. if ncplayer.isPlay() {
  194. ncplayer.playerPause()
  195. timerAutoHide?.invalidate()
  196. } else {
  197. ncplayer.playerPlay()
  198. startTimerAutoHide()
  199. }
  200. }
  201. @IBAction func tapMute(_ sender: Any) {
  202. guard let ncplayer = ncplayer else { return }
  203. if CCUtility.getAudioVolume() > 0 {
  204. CCUtility.setAudioVolume(0)
  205. ncplayer.setVolumeAudio(0)
  206. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .white), for: .normal)
  207. } else {
  208. CCUtility.setAudioVolume(100)
  209. ncplayer.setVolumeAudio(100)
  210. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
  211. }
  212. startTimerAutoHide()
  213. }
  214. @IBAction func tapForward(_ sender: Any) {
  215. guard let ncplayer = ncplayer else { return }
  216. ncplayer.jumpForward(10)
  217. startTimerAutoHide()
  218. }
  219. @IBAction func tapBack(_ sender: Any) {
  220. guard let ncplayer = ncplayer else { return }
  221. ncplayer.jumpBackward(10)
  222. startTimerAutoHide()
  223. }
  224. }