NCViewerVideoToolBar.swift 9.6 KB

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