NCViewerVideoToolBar.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. var player: AVPlayer?
  33. private let seekDuration: Float64 = 15
  34. private var timerAutoHide: Timer?
  35. override func willMove(toWindow newWindow: UIWindow?) {
  36. super.willMove(toWindow: newWindow)
  37. if newWindow != nil {
  38. let blurEffect = UIBlurEffect(style: .dark)
  39. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  40. self.layer.cornerRadius = 15
  41. self.layer.masksToBounds = true
  42. blurEffectView.frame = self.bounds
  43. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  44. self.insertSubview(blurEffectView, at:0)
  45. playbackSlider.value = 0
  46. playbackSlider.minimumValue = 0
  47. playbackSlider.maximumValue = 0
  48. playbackSlider.isContinuous = true
  49. playbackSlider.tintColor = .lightGray
  50. labelCurrentTime.text = stringFromTimeInterval(interval: 0)
  51. labelCurrentTime.textColor = .lightGray
  52. labelOverallDuration.text = stringFromTimeInterval(interval: 0)
  53. labelOverallDuration.textColor = .lightGray
  54. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.15", color: .white), for: .normal)
  55. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.15", color: .white), for: .normal)
  56. setToolBar()
  57. }
  58. }
  59. func setPlayer(player: AVPlayer?) {
  60. self.player = player
  61. let duration: CMTime = (player?.currentItem?.asset.duration)!
  62. let durationSeconds: Float64 = CMTimeGetSeconds(duration)
  63. playbackSlider.value = 0
  64. playbackSlider.minimumValue = 0
  65. playbackSlider.maximumValue = Float(durationSeconds)
  66. playbackSlider.action(for: .valueChanged) { _ in
  67. let seconds : Int64 = Int64(self.playbackSlider.value)
  68. let targetTime:CMTime = CMTimeMake(value: seconds, timescale: 1)
  69. self.player?.seek(to: targetTime)
  70. if self.player?.rate == 0 {
  71. self.player?.play()
  72. }
  73. }
  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 showToolBar() {
  89. updateOutlet()
  90. self.isHidden = false
  91. timerAutoHide?.invalidate()
  92. timerAutoHide = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(hideToolBar), userInfo: nil, repeats: true)
  93. }
  94. public func setToolBar() {
  95. if player?.rate == 1 {
  96. playButton.setImage(NCUtility.shared.loadImage(named: "pause.fill", color: .white), for: .normal)
  97. } else {
  98. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .white), for: .normal)
  99. }
  100. if CCUtility.getAudioMute() {
  101. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .white), for: .normal)
  102. } else {
  103. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
  104. }
  105. }
  106. private func updateOutlet() {
  107. if let duration = player?.currentItem?.asset.duration, let currentTime = player?.currentTime() {
  108. let durationSeconds: Float64 = CMTimeGetSeconds(duration)
  109. let currentSeconds: Float64 = CMTimeGetSeconds(currentTime)
  110. self.playbackSlider.value = Float(currentSeconds)
  111. self.labelCurrentTime.text = self.stringFromTimeInterval(interval: currentSeconds)
  112. self.labelOverallDuration.text = "-" + self.stringFromTimeInterval(interval: durationSeconds - currentSeconds)
  113. }
  114. }
  115. //MARK: - Action
  116. @IBAction func playerPause(_ sender: Any) {
  117. if player?.timeControlStatus == .playing {
  118. player?.pause()
  119. } else if player?.timeControlStatus == .paused {
  120. player?.play()
  121. }
  122. }
  123. @IBAction func setMute(_ sender: Any) {
  124. let mute = CCUtility.getAudioMute()
  125. CCUtility.setAudioMute(!mute)
  126. player?.isMuted = !mute
  127. setToolBar()
  128. }
  129. @IBAction func forwardButtonSec(_ sender: Any) {
  130. guard let player = self.player else { return }
  131. if let duration = player.currentItem?.duration {
  132. let playerCurrentTime = CMTimeGetSeconds(player.currentTime())
  133. let newTime = playerCurrentTime + seekDuration
  134. if newTime < CMTimeGetSeconds(duration) {
  135. let selectedTime: CMTime = CMTimeMake(value: Int64(newTime * 1000 as Float64), timescale: 1000)
  136. player.seek(to: selectedTime)
  137. player.pause()
  138. player.play()
  139. }
  140. }
  141. }
  142. @IBAction func backButtonSec(_ sender: Any) {
  143. guard let player = self.player else { return }
  144. let playerCurrenTime = CMTimeGetSeconds(player.currentTime())
  145. var newTime = playerCurrenTime - seekDuration
  146. if newTime < 0 { newTime = 0 }
  147. let selectedTime: CMTime = CMTimeMake(value: Int64(newTime * 1000 as Float64), timescale: 1000)
  148. player.seek(to: selectedTime)
  149. player.pause()
  150. player.play()
  151. }
  152. //MARK: - Algorithms
  153. func stringFromTimeInterval(interval: TimeInterval) -> String {
  154. let interval = Int(interval)
  155. let seconds = interval % 60
  156. let minutes = (interval / 60) % 60
  157. let hours = (interval / 3600)
  158. if hours > 0 {
  159. return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
  160. } else {
  161. return String(format: "%02d:%02d", minutes, seconds)
  162. }
  163. }
  164. }