NCViewerVideoToolBar.swift 8.4 KB

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