NCPlayerToolBar.swift 9.3 KB

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