NCViewerVideoToolBar.swift 9.1 KB

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