NCViewerVideoToolBar.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 viewerVideo: NCViewerVideo!
  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. func setBarPlayer(viewerVideo: NCViewerVideo) {
  72. self.viewerVideo = viewerVideo
  73. playbackSlider.value = 0
  74. playbackSlider.minimumValue = 0
  75. playbackSlider.maximumValue = Float(viewerVideo.getVideoDurationSeconds())
  76. playbackSlider.addTarget(self, action: #selector(onSliderValChanged(slider:event:)), for: .valueChanged)
  77. labelCurrentTime.text = NCUtility.shared.stringFromTimeInterval(interval: 0)
  78. labelOverallDuration.text = "-" + NCUtility.shared.stringFromTimeInterval(interval:viewerVideo.getVideoDurationSeconds())
  79. updateToolBar()
  80. viewerVideo.player?.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, preferredTimescale: 1), queue: .main, using: { (CMTime) in
  81. if viewerVideo.player?.currentItem?.status == .readyToPlay {
  82. if self.isHidden == false {
  83. self.updateToolBar()
  84. }
  85. }
  86. })
  87. }
  88. @objc public func hideToolBar() {
  89. updateToolBar()
  90. UIView.animate(withDuration: 0.3, animations: {
  91. self.alpha = 0
  92. }, completion: { (value: Bool) in
  93. self.isHidden = true
  94. })
  95. }
  96. @discardableResult
  97. @objc public func showToolBar(metadata: tableMetadata) -> Bool {
  98. if !self.isHidden { return false}
  99. if metadata.livePhoto { return false}
  100. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  101. updateToolBar()
  102. UIView.animate(withDuration: 0.3, animations: {
  103. self.alpha = 1
  104. }, completion: { (value: Bool) in
  105. self.isHidden = false
  106. })
  107. return true
  108. } else {
  109. return false
  110. }
  111. }
  112. public func updateToolBar() {
  113. var namedPlay = "play.fill"
  114. if viewerVideo.player?.rate == 1 { namedPlay = "pause.fill"}
  115. let currentSeconds = viewerVideo.getVideoCurrentSeconds()
  116. let durationSeconds = viewerVideo.getVideoDurationSeconds()
  117. playbackSlider.value = Float(currentSeconds)
  118. playbackSlider.isEnabled = true
  119. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.15", color: .white), for: .normal)
  120. backButton.isEnabled = true
  121. if #available(iOS 13.0, *) {
  122. playButton.setImage(NCUtility.shared.loadImage(named: namedPlay, color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  123. } else {
  124. playButton.setImage(NCUtility.shared.loadImage(named: namedPlay, color: .white), for: .normal)
  125. }
  126. playButton.isEnabled = true
  127. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.15", color: .white), for: .normal)
  128. forwardButton.isEnabled = true
  129. if CCUtility.getAudioMute() {
  130. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .white), for: .normal)
  131. } else {
  132. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
  133. }
  134. muteButton.isEnabled = true
  135. labelCurrentTime.text = NCUtility.shared.stringFromTimeInterval(interval: currentSeconds)
  136. labelOverallDuration.text = "-" + NCUtility.shared.stringFromTimeInterval(interval: durationSeconds - currentSeconds)
  137. }
  138. //MARK: - Event / Gesture
  139. @objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
  140. if let touchEvent = event.allTouches?.first {
  141. let seconds: Int64 = Int64(self.playbackSlider.value)
  142. let targetTime: CMTime = CMTimeMake(value: seconds, timescale: 1)
  143. switch touchEvent.phase {
  144. case .began:
  145. wasInPlay = viewerVideo.player?.rate == 1 ? true : false
  146. viewerVideo.videoPause()
  147. playbackSliderEvent = .began
  148. case .moved:
  149. viewerVideo.videoSeek(time: targetTime)
  150. playbackSliderEvent = .moved
  151. case .ended:
  152. viewerVideo.videoSeek(time: targetTime)
  153. if wasInPlay {
  154. viewerVideo.videoPlay()
  155. }
  156. playbackSliderEvent = .ended
  157. default:
  158. break
  159. }
  160. }
  161. }
  162. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  163. hideToolBar()
  164. }
  165. //MARK: - Action
  166. @IBAction func buttonTouchInside(_ sender: UIButton) {
  167. hideToolBar()
  168. }
  169. @IBAction func playerPause(_ sender: Any) {
  170. if viewerVideo.player?.timeControlStatus == .playing {
  171. viewerVideo.videoPause()
  172. } else if viewerVideo.player?.timeControlStatus == .paused {
  173. viewerVideo.videoPlay()
  174. }
  175. }
  176. @IBAction func setMute(_ sender: Any) {
  177. let mute = CCUtility.getAudioMute()
  178. CCUtility.setAudioMute(!mute)
  179. viewerVideo.player?.isMuted = !mute
  180. updateToolBar()
  181. }
  182. @IBAction func forwardButtonSec(_ sender: Any) {
  183. guard let player = viewerVideo.player else { return }
  184. let playerCurrentTime = CMTimeGetSeconds(player.currentTime())
  185. let newTime = playerCurrentTime + seekDuration
  186. if newTime < viewerVideo.getVideoDurationSeconds() {
  187. let time: CMTime = CMTimeMake(value: Int64(newTime * 1000 as Float64), timescale: 1000)
  188. viewerVideo.videoSeek(time: time)
  189. }
  190. }
  191. @IBAction func backButtonSec(_ sender: Any) {
  192. guard let player = viewerVideo.player else { return }
  193. let playerCurrenTime = CMTimeGetSeconds(player.currentTime())
  194. var newTime = playerCurrenTime - seekDuration
  195. if newTime < 0 { newTime = 0 }
  196. let time: CMTime = CMTimeMake(value: Int64(newTime * 1000 as Float64), timescale: 1000)
  197. viewerVideo.videoSeek(time: time)
  198. }
  199. }