NCViewerVideoToolBar.swift 8.6 KB

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