NCPlayerToolBar.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 NCCommunication
  25. class NCPlayerToolBar: UIView {
  26. @IBOutlet weak var playButton: UIButton!
  27. @IBOutlet weak var muteButton: UIButton!
  28. @IBOutlet weak var forwardButton: UIButton!
  29. @IBOutlet weak var backButton: UIButton!
  30. @IBOutlet weak var playbackSlider: UISlider!
  31. @IBOutlet weak var labelOverallDuration: UILabel!
  32. @IBOutlet weak var labelCurrentTime: UILabel!
  33. enum sliderEventType {
  34. case began
  35. case ended
  36. case moved
  37. }
  38. private var player: NCPlayer?
  39. private var wasInPlay: Bool = false
  40. private var playbackSliderEvent: sliderEventType = .ended
  41. private let seekDuration: Float64 = 15
  42. // MARK: - View Life Cycle
  43. override func awakeFromNib() {
  44. super.awakeFromNib()
  45. // for disable gesture of UIPageViewController
  46. let panRecognizer = UIPanGestureRecognizer(target: self, action: nil)
  47. addGestureRecognizer(panRecognizer)
  48. let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSingleTapWith(gestureRecognizer:)))
  49. addGestureRecognizer(singleTapGestureRecognizer)
  50. let blurEffect = UIBlurEffect(style: .dark)
  51. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  52. self.layer.cornerRadius = 15
  53. self.layer.masksToBounds = true
  54. blurEffectView.frame = self.bounds
  55. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  56. self.insertSubview(blurEffectView, at:0)
  57. playbackSlider.value = 0
  58. playbackSlider.minimumValue = 0
  59. playbackSlider.maximumValue = 0
  60. playbackSlider.isContinuous = true
  61. playbackSlider.tintColor = .lightGray
  62. labelCurrentTime.text = NCUtility.shared.stringFromTimeInterval(interval: 0)
  63. labelCurrentTime.textColor = .lightGray
  64. labelOverallDuration.text = NCUtility.shared.stringFromTimeInterval(interval: 0)
  65. labelOverallDuration.textColor = .lightGray
  66. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.15", color: .lightGray), for: .normal)
  67. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .lightGray), for: .normal)
  68. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.15", color: .lightGray), for: .normal)
  69. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .lightGray), for: .normal)
  70. }
  71. deinit {
  72. print("deinit NCPlayerToolBar")
  73. }
  74. func setBarPlayer(player: NCPlayer) {
  75. self.player = player
  76. playbackSlider.value = 0
  77. playbackSlider.minimumValue = 0
  78. playbackSlider.maximumValue = Float(player.getVideoDurationSeconds())
  79. playbackSlider.addTarget(self, action: #selector(onSliderValChanged(slider:event:)), for: .valueChanged)
  80. labelCurrentTime.text = NCUtility.shared.stringFromTimeInterval(interval: 0)
  81. labelOverallDuration.text = "-" + NCUtility.shared.stringFromTimeInterval(interval:player.getVideoDurationSeconds())
  82. updateToolBar()
  83. player.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, preferredTimescale: 1), queue: .main, using: { (CMTime) in
  84. if player.currentItem?.status == .readyToPlay {
  85. if self.isHidden == false {
  86. self.updateToolBar()
  87. }
  88. }
  89. })
  90. }
  91. @objc public func hideToolBar() {
  92. updateToolBar()
  93. UIView.animate(withDuration: 0.3, animations: {
  94. self.alpha = 0
  95. }, completion: { (value: Bool) in
  96. self.isHidden = true
  97. })
  98. }
  99. @discardableResult
  100. @objc public func showToolBar(metadata: tableMetadata, detailView: NCViewerMediaDetailView?) -> Bool {
  101. if !self.isHidden { return false}
  102. if metadata.livePhoto { return false}
  103. if let detailView = detailView {
  104. if detailView.isShow() { return false }
  105. }
  106. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  107. updateToolBar()
  108. UIView.animate(withDuration: 0.3, animations: {
  109. self.alpha = 1
  110. }, completion: { (value: Bool) in
  111. self.isHidden = false
  112. })
  113. return true
  114. } else {
  115. return false
  116. }
  117. }
  118. public func updateToolBar() {
  119. var namedPlay = "play.fill"
  120. if player?.rate == 1 { namedPlay = "pause.fill"}
  121. let currentSeconds = player?.getVideoCurrentSeconds() ?? 0
  122. let durationSeconds = player?.getVideoDurationSeconds() ?? 0
  123. playbackSlider.value = Float(currentSeconds)
  124. playbackSlider.isEnabled = true
  125. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.15", color: .white), for: .normal)
  126. backButton.isEnabled = true
  127. if #available(iOS 13.0, *) {
  128. playButton.setImage(NCUtility.shared.loadImage(named: namedPlay, color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  129. } else {
  130. playButton.setImage(NCUtility.shared.loadImage(named: namedPlay, color: .white), for: .normal)
  131. }
  132. playButton.isEnabled = true
  133. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.15", color: .white), for: .normal)
  134. forwardButton.isEnabled = true
  135. if CCUtility.getAudioMute() {
  136. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .white), for: .normal)
  137. } else {
  138. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
  139. }
  140. muteButton.isEnabled = true
  141. labelCurrentTime.text = NCUtility.shared.stringFromTimeInterval(interval: currentSeconds)
  142. labelOverallDuration.text = "-" + NCUtility.shared.stringFromTimeInterval(interval: durationSeconds - currentSeconds)
  143. }
  144. //MARK: - Event / Gesture
  145. @objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
  146. if let touchEvent = event.allTouches?.first {
  147. let seconds: Int64 = Int64(self.playbackSlider.value)
  148. let targetTime: CMTime = CMTimeMake(value: seconds, timescale: 1)
  149. switch touchEvent.phase {
  150. case .began:
  151. wasInPlay = player?.rate == 1 ? true : false
  152. player?.videoPause()
  153. playbackSliderEvent = .began
  154. case .moved:
  155. player?.videoSeek(time: targetTime)
  156. playbackSliderEvent = .moved
  157. case .ended:
  158. player?.videoSeek(time: targetTime)
  159. if wasInPlay {
  160. player?.videoPlay()
  161. }
  162. playbackSliderEvent = .ended
  163. default:
  164. break
  165. }
  166. }
  167. }
  168. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  169. hideToolBar()
  170. }
  171. //MARK: - Action
  172. @IBAction func buttonTouchInside(_ sender: UIButton) {
  173. hideToolBar()
  174. }
  175. @IBAction func playerPause(_ sender: Any) {
  176. if player?.timeControlStatus == .playing {
  177. player?.videoPause()
  178. } else if player?.timeControlStatus == .paused {
  179. player?.videoPlay()
  180. } else if player?.timeControlStatus == .waitingToPlayAtSpecifiedRate {
  181. player?.deleteLocalFile()
  182. print("timeControlStatus.waitingToPlayAtSpecifiedRate")
  183. if let reason = player?.reasonForWaitingToPlay {
  184. switch reason {
  185. case .evaluatingBufferingRate:
  186. print("reasonForWaitingToPlay.evaluatingBufferingRate")
  187. case .toMinimizeStalls:
  188. print("reasonForWaitingToPlay.toMinimizeStalls")
  189. case .noItemToPlay:
  190. print("reasonForWaitingToPlay.noItemToPlay")
  191. default:
  192. print("Unknown \(reason)")
  193. }
  194. }
  195. }
  196. }
  197. @IBAction func setMute(_ sender: Any) {
  198. let mute = CCUtility.getAudioMute()
  199. CCUtility.setAudioMute(!mute)
  200. player?.isMuted = !mute
  201. updateToolBar()
  202. }
  203. @IBAction func forwardButtonSec(_ sender: Any) {
  204. guard let player = player else { return }
  205. let playerCurrentTime = CMTimeGetSeconds(player.currentTime())
  206. let newTime = playerCurrentTime + seekDuration
  207. if newTime < player.getVideoDurationSeconds() {
  208. let time: CMTime = CMTimeMake(value: Int64(newTime * 1000 as Float64), timescale: 1000)
  209. player.videoSeek(time: time)
  210. }
  211. }
  212. @IBAction func backButtonSec(_ sender: Any) {
  213. guard let player = player else { return }
  214. let playerCurrenTime = CMTimeGetSeconds(player.currentTime())
  215. var newTime = playerCurrenTime - seekDuration
  216. if newTime < 0 { newTime = 0 }
  217. let time: CMTime = CMTimeMake(value: Int64(newTime * 1000 as Float64), timescale: 1000)
  218. player.videoSeek(time: time)
  219. }
  220. }