NCViewerVideoToolBar.swift 6.8 KB

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