NCViewerVideoToolBar.swift 9.1 KB

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