NCPlayerToolBar.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. private var ncplayer: NCPlayer?
  46. private var metadata: tableMetadata?
  47. private var wasInPlay: Bool = false
  48. private var playbackSliderEvent: sliderEventType = .ended
  49. private var timerAutoHide: Timer?
  50. private var timerAutoHideSeconds: Double {
  51. get {
  52. if NCUtility.shared.isSimulator() { // for test
  53. return 15
  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. playbackSlider.isEnabled = false
  82. labelCurrentTime.text = NCUtility.shared.stringFromTime(.zero)
  83. labelCurrentTime.textColor = .lightGray
  84. labelLeftTime.text = NCUtility.shared.stringFromTime(.zero)
  85. labelLeftTime.textColor = .lightGray
  86. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .lightGray), for: .normal)
  87. muteButton.isEnabled = false
  88. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .lightGray, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  89. playButton.isEnabled = false
  90. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.10", color: .lightGray), for: .normal)
  91. backButton.isEnabled = false
  92. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.10", color: .lightGray), for: .normal)
  93. forwardButton.isEnabled = false
  94. }
  95. required init?(coder aDecoder: NSCoder) {
  96. super.init(coder: aDecoder)
  97. }
  98. deinit {
  99. print("deinit NCPlayerToolBar")
  100. }
  101. // MARK: -
  102. func setBarPlayer(ncplayer: NCPlayer, position: Float, metadata: tableMetadata) {
  103. self.ncplayer = ncplayer
  104. self.metadata = metadata
  105. playbackSlider.value = position
  106. playbackSlider.addTarget(self, action: #selector(onSliderValChanged(slider:event:)), for: .valueChanged)
  107. labelCurrentTime.text = ncplayer.player?.time.stringValue
  108. labelLeftTime.text = ncplayer.player?.remainingTime?.stringValue
  109. show(enableTimerAutoHide: false)
  110. update(position: position)
  111. }
  112. public func buffering() {
  113. muteButton.isEnabled = false
  114. playButton.isEnabled = false
  115. forwardButton.isEnabled = false
  116. backButton.isEnabled = false
  117. playbackSlider.isEnabled = false
  118. }
  119. public func update(position: Float?) {
  120. guard let ncplayer = self.ncplayer,
  121. let length = ncplayer.player?.media?.length.intValue,
  122. let position = position
  123. else { return }
  124. let positionInSecond = position * Float(length / 1000)
  125. // SAVE POSITION
  126. if position > 0 {
  127. ncplayer.savePosition(position)
  128. }
  129. // MUTE
  130. if let muteButton = muteButton {
  131. let audio = CCUtility.getAudioVolume()
  132. if audio == 0 {
  133. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .white), for: .normal)
  134. } else {
  135. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
  136. }
  137. muteButton.isEnabled = true
  138. }
  139. // SLIDER TIME (START - END)
  140. playbackSlider.value = position
  141. playbackSlider.isEnabled = true
  142. labelCurrentTime.text = ncplayer.player?.time.stringValue
  143. labelLeftTime.text = ncplayer.player?.remainingTime?.stringValue
  144. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyPlaybackDuration] = length / 1000
  145. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = positionInSecond
  146. // BACK FORWARD
  147. if length > 0 {
  148. forwardButton.isEnabled = true
  149. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.10", color: .white), for: .normal)
  150. backButton.isEnabled = true
  151. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.10", color: .white), for: .normal)
  152. } else {
  153. backButton.isEnabled = false
  154. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.10", color: .lightGray), for: .normal)
  155. forwardButton.isEnabled = false
  156. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.10", color: .lightGray), for: .normal)
  157. }
  158. // PLAY
  159. if ncplayer.isPlay() {
  160. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 1
  161. } else {
  162. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0
  163. }
  164. let namedPlay = ncplayer.isPlay() ? "pause.fill" : "play.fill"
  165. playButton.setImage(NCUtility.shared.loadImage(named: namedPlay, color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  166. playButton.isEnabled = true
  167. }
  168. // MARK: -
  169. public func show(enableTimerAutoHide: Bool = false) {
  170. guard let metadata = self.metadata, ncplayer != nil, !metadata.livePhoto else { return }
  171. if metadata.classFile != NKCommon.TypeClassFile.video.rawValue && metadata.classFile != NKCommon.TypeClassFile.audio.rawValue { return }
  172. timerAutoHide?.invalidate()
  173. if enableTimerAutoHide {
  174. startTimerAutoHide()
  175. }
  176. if !self.isHidden { return }
  177. UIView.animate(withDuration: 0.3, animations: {
  178. self.alpha = 1
  179. }, completion: { (_: Bool) in
  180. self.isHidden = false
  181. })
  182. update(position: ncplayer?.player?.position)
  183. }
  184. func isShow() -> Bool {
  185. return !self.isHidden
  186. }
  187. public func hide() {
  188. UIView.animate(withDuration: 0.3, animations: {
  189. self.alpha = 0
  190. }, completion: { (_: Bool) in
  191. self.isHidden = true
  192. })
  193. }
  194. @objc private func automaticHide() {
  195. if let metadata = self.metadata {
  196. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterHidePlayerToolBar, userInfo: ["ocId": metadata.ocId])
  197. }
  198. }
  199. private func startTimerAutoHide() {
  200. timerAutoHide?.invalidate()
  201. timerAutoHide = Timer.scheduledTimer(timeInterval: timerAutoHideSeconds, target: self, selector: #selector(automaticHide), userInfo: nil, repeats: false)
  202. }
  203. private func reStartTimerAutoHide() {
  204. if let timerAutoHide = timerAutoHide, timerAutoHide.isValid {
  205. startTimerAutoHide()
  206. }
  207. }
  208. func skip(seconds: Float) {
  209. guard let ncplayer = ncplayer,
  210. let player = ncplayer.player,
  211. let length = player.media?.length.intValue
  212. else { return }
  213. let currentPosition = player.position * Float(length / 1000)
  214. var newPosition = (currentPosition + seconds) / Float(length / 1000)
  215. if newPosition < 0 || newPosition > 1 {
  216. newPosition = 0
  217. }
  218. ncplayer.videoSeek(position: newPosition)
  219. update(position: newPosition)
  220. reStartTimerAutoHide()
  221. }
  222. func stopTimerAutoHide() {
  223. timerAutoHide?.invalidate()
  224. }
  225. // MARK: - Event / Gesture
  226. @objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
  227. guard let touchEvent = event.allTouches?.first,
  228. let ncplayer = ncplayer
  229. else { return }
  230. let newPosition = self.playbackSlider.value
  231. switch touchEvent.phase {
  232. case .began:
  233. ncplayer.playerPause()
  234. playbackSliderEvent = .began
  235. case .moved:
  236. playbackSliderEvent = .moved
  237. case .ended:
  238. ncplayer.playerPlay()
  239. ncplayer.videoSeek(position: newPosition)
  240. playbackSliderEvent = .ended
  241. default:
  242. break
  243. }
  244. reStartTimerAutoHide()
  245. }
  246. // MARK: - Action
  247. @objc func tapTopToolBarWith(gestureRecognizer: UITapGestureRecognizer) { }
  248. @objc func tapToolBarWith(gestureRecognizer: UITapGestureRecognizer) { }
  249. @IBAction func tapPlayerPause(_ sender: Any) {
  250. guard let ncplayer = ncplayer else { return }
  251. if ncplayer.isPlay() {
  252. ncplayer.playerPause()
  253. timerAutoHide?.invalidate()
  254. } else {
  255. ncplayer.playerPlay()
  256. startTimerAutoHide()
  257. }
  258. }
  259. @IBAction func tapMute(_ sender: Any) {
  260. let volume = CCUtility.getAudioVolume()
  261. if volume > 0 {
  262. CCUtility.setAudioVolume(0)
  263. ncplayer?.player?.audio?.volume = 0
  264. } else {
  265. CCUtility.setAudioVolume(100)
  266. ncplayer?.player?.audio?.volume = 100
  267. }
  268. update(position: ncplayer?.player?.position)
  269. reStartTimerAutoHide()
  270. }
  271. @IBAction func tapForward(_ sender: Any) {
  272. skip(seconds: 10)
  273. }
  274. @IBAction func tapBack(_ sender: Any) {
  275. skip(seconds: -10)
  276. }
  277. }