NCViewerVideoToolBar.swift 8.7 KB

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