NCViewerVideoToolBar.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 playbackSlider: UISlider!
  28. @IBOutlet weak var labelOverallDuration: UILabel!
  29. @IBOutlet weak var labelCurrentTime: UILabel!
  30. var player: AVPlayer?
  31. override func willMove(toWindow newWindow: UIWindow?) {
  32. super.willMove(toWindow: newWindow)
  33. if newWindow != nil {
  34. let blurEffect = UIBlurEffect(style: .dark)
  35. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  36. self.layer.cornerRadius = 15
  37. self.layer.masksToBounds = true
  38. blurEffectView.frame = self.bounds
  39. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  40. self.insertSubview(blurEffectView, at:0)
  41. }
  42. }
  43. func setPlayer(player: AVPlayer?) {
  44. self.player = player
  45. let duration: CMTime = (player?.currentItem?.asset.duration)!
  46. let durationSeconds: Float64 = CMTimeGetSeconds(duration)
  47. playbackSlider.minimumValue = 0
  48. playbackSlider.maximumValue = Float(durationSeconds)
  49. playbackSlider.isContinuous = true
  50. playbackSlider.action(for: .valueChanged) { _ in
  51. let seconds : Int64 = Int64(self.playbackSlider.value)
  52. let targetTime:CMTime = CMTimeMake(value: seconds, timescale: 1)
  53. self.player?.seek(to: targetTime)
  54. if self.player?.rate == 0 {
  55. self.player?.play()
  56. }
  57. }
  58. labelCurrentTime.text = stringFromTimeInterval(interval: 0)
  59. labelCurrentTime.textColor = .lightGray
  60. labelOverallDuration.text = "-" + stringFromTimeInterval(interval: durationSeconds)
  61. labelOverallDuration.textColor = .lightGray
  62. player?.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, preferredTimescale: 1), queue: .main, using: { (CMTime) in
  63. if self.player?.currentItem?.status == .readyToPlay {
  64. let currentSeconds: Float64 = CMTimeGetSeconds(self.player!.currentTime())
  65. self.playbackSlider.value = Float(currentSeconds)
  66. self.labelCurrentTime.text = self.stringFromTimeInterval(interval: currentSeconds)
  67. self.labelOverallDuration.text = "-" + self.stringFromTimeInterval(interval: durationSeconds - currentSeconds)
  68. }
  69. })
  70. }
  71. func setToolBar() {
  72. let mute = CCUtility.getAudioMute()
  73. if player?.rate == 1 {
  74. playButton.setImage(NCUtility.shared.loadImage(named: "pause.fill", color: .white), for: .normal)
  75. } else {
  76. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .white), for: .normal)
  77. }
  78. if mute {
  79. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .white), for: .normal)
  80. } else {
  81. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
  82. }
  83. }
  84. @IBAction func playerPause(_ sender: Any) {
  85. if player?.timeControlStatus == .playing {
  86. player?.pause()
  87. } else if player?.timeControlStatus == .paused {
  88. player?.play()
  89. }
  90. }
  91. @IBAction func setMute(_ sender: Any) {
  92. let mute = CCUtility.getAudioMute()
  93. CCUtility.setAudioMute(!mute)
  94. player?.isMuted = !mute
  95. setToolBar()
  96. }
  97. @objc func playbackSliderValueChanged(_ playbackSlider:UISlider) {
  98. let seconds : Int64 = Int64(playbackSlider.value)
  99. let targetTime:CMTime = CMTimeMake(value: seconds, timescale: 1)
  100. player?.seek(to: targetTime)
  101. if player?.rate == 0 {
  102. player?.play()
  103. }
  104. }
  105. func stringFromTimeInterval(interval: TimeInterval) -> String {
  106. let interval = Int(interval)
  107. let seconds = interval % 60
  108. let minutes = (interval / 60) % 60
  109. let hours = (interval / 3600)
  110. if hours > 0 {
  111. return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
  112. } else {
  113. return String(format: "%02d:%02d", minutes, seconds)
  114. }
  115. }
  116. }