NCPlayerToolBar.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 150
  54. } else {
  55. return 3.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. print("deinit NCPlayerToolBar")
  93. }
  94. // MARK: -
  95. func setBarPlayer(ncplayer: NCPlayer, position: Float, metadata: tableMetadata) {
  96. self.ncplayer = ncplayer
  97. self.metadata = metadata
  98. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  99. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0
  100. playbackSlider.value = position
  101. playbackSlider.addTarget(self, action: #selector(onSliderValChanged(slider:event:)), for: .valueChanged)
  102. labelCurrentTime.text = ncplayer.player?.time.stringValue
  103. labelLeftTime.text = ncplayer.player?.remainingTime?.stringValue
  104. if CCUtility.getAudioVolume() == 0 {
  105. ncplayer.setVolumeAudio(0)
  106. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .white), for: .normal)
  107. } else {
  108. ncplayer.setVolumeAudio(100)
  109. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
  110. }
  111. show(enableTimerAutoHide: false)
  112. }
  113. public func update() {
  114. guard let ncplayer = self.ncplayer,
  115. let length = ncplayer.player?.media?.length.intValue,
  116. let position = ncplayer.player?.position
  117. else { return }
  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 disableAllControl() {
  129. muteButton.isEnabled = false
  130. playButton.isEnabled = false
  131. forwardButton.isEnabled = false
  132. backButton.isEnabled = false
  133. playbackSlider.isEnabled = false
  134. }
  135. // MARK: -
  136. public func show(enableTimerAutoHide: Bool = false) {
  137. timerAutoHide?.invalidate()
  138. if enableTimerAutoHide {
  139. startTimerAutoHide()
  140. }
  141. if !self.isHidden { return }
  142. UIView.animate(withDuration: 0.3, animations: {
  143. self.alpha = 1
  144. }, completion: { (_: Bool) in
  145. self.isHidden = false
  146. })
  147. }
  148. func isShow() -> Bool {
  149. return !self.isHidden
  150. }
  151. public func hide() {
  152. UIView.animate(withDuration: 0.3, animations: {
  153. self.alpha = 0
  154. }, completion: { (_: Bool) in
  155. self.isHidden = true
  156. })
  157. }
  158. @objc private func automaticHide() {
  159. if let metadata = self.metadata {
  160. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterHidePlayerToolBar, userInfo: ["ocId": metadata.ocId])
  161. }
  162. }
  163. private func startTimerAutoHide() {
  164. timerAutoHide?.invalidate()
  165. timerAutoHide = Timer.scheduledTimer(timeInterval: timerAutoHideSeconds, target: self, selector: #selector(automaticHide), userInfo: nil, repeats: false)
  166. }
  167. private func reStartTimerAutoHide() {
  168. if let timerAutoHide = timerAutoHide, timerAutoHide.isValid {
  169. startTimerAutoHide()
  170. }
  171. }
  172. func stopTimerAutoHide() {
  173. timerAutoHide?.invalidate()
  174. }
  175. func playButtonPause() {
  176. playButton.setImage(NCUtility.shared.loadImage(named: "pause.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  177. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 1
  178. }
  179. func playButtonPlay() {
  180. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  181. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0
  182. }
  183. // MARK: - Event / Gesture
  184. @objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
  185. guard let touchEvent = event.allTouches?.first,
  186. let ncplayer = ncplayer
  187. else { return }
  188. let newPosition = playbackSlider.value
  189. switch touchEvent.phase {
  190. case .began:
  191. playbackSliderEvent = .began
  192. case .moved:
  193. ncplayer.playerPosition(newPosition)
  194. playbackSliderEvent = .moved
  195. case .ended:
  196. ncplayer.playerPosition(newPosition)
  197. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  198. self.playbackSliderEvent = .ended
  199. }
  200. default:
  201. break
  202. }
  203. reStartTimerAutoHide()
  204. }
  205. // MARK: - Action
  206. @objc func tapTopToolBarWith(gestureRecognizer: UITapGestureRecognizer) { }
  207. @objc func tapToolBarWith(gestureRecognizer: UITapGestureRecognizer) { }
  208. @IBAction func tapPlayerPause(_ sender: Any) {
  209. guard let ncplayer = ncplayer else { return }
  210. if ncplayer.isPlay() {
  211. ncplayer.playerPause()
  212. timerAutoHide?.invalidate()
  213. } else {
  214. ncplayer.playerPlay()
  215. startTimerAutoHide()
  216. }
  217. }
  218. @IBAction func tapMute(_ sender: Any) {
  219. guard let ncplayer = ncplayer else { return }
  220. if CCUtility.getAudioVolume() > 0 {
  221. CCUtility.setAudioVolume(0)
  222. ncplayer.setVolumeAudio(0)
  223. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .white), for: .normal)
  224. } else {
  225. CCUtility.setAudioVolume(100)
  226. ncplayer.setVolumeAudio(100)
  227. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
  228. }
  229. reStartTimerAutoHide()
  230. }
  231. @IBAction func tapForward(_ sender: Any) {
  232. guard let ncplayer = ncplayer else { return }
  233. ncplayer.jumpForward(10)
  234. reStartTimerAutoHide()
  235. }
  236. @IBAction func tapBack(_ sender: Any) {
  237. guard let ncplayer = ncplayer else { return }
  238. ncplayer.jumpBackward(10)
  239. reStartTimerAutoHide()
  240. }
  241. }