NCPlayerToolBar.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //
  2. // NCPlayerToolBar.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 NCPlayerToolBar: 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. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  39. private var ncplayer: NCPlayer?
  40. private var wasInPlay: Bool = false
  41. private var playbackSliderEvent: sliderEventType = .ended
  42. private let seekDuration: Float64 = 15
  43. // MARK: - View Life Cycle
  44. override func awakeFromNib() {
  45. super.awakeFromNib()
  46. // for disable gesture of UIPageViewController
  47. let panRecognizer = UIPanGestureRecognizer(target: self, action: nil)
  48. addGestureRecognizer(panRecognizer)
  49. let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSingleTapWith(gestureRecognizer:)))
  50. addGestureRecognizer(singleTapGestureRecognizer)
  51. let blurEffect = UIBlurEffect(style: .dark)
  52. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  53. self.layer.cornerRadius = 15
  54. self.layer.masksToBounds = true
  55. blurEffectView.frame = self.bounds
  56. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  57. self.insertSubview(blurEffectView, at:0)
  58. playbackSlider.value = 0
  59. playbackSlider.minimumValue = 0
  60. playbackSlider.maximumValue = 0
  61. playbackSlider.isContinuous = true
  62. playbackSlider.tintColor = .lightGray
  63. labelCurrentTime.text = NCUtility.shared.stringFromTime(.zero)
  64. labelCurrentTime.textColor = .lightGray
  65. labelOverallDuration.text = NCUtility.shared.stringFromTime(.zero)
  66. labelOverallDuration.textColor = .lightGray
  67. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.15", color: .lightGray), for: .normal)
  68. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .lightGray), for: .normal)
  69. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.15", color: .lightGray), for: .normal)
  70. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .lightGray), for: .normal)
  71. }
  72. deinit {
  73. print("deinit NCPlayerToolBar")
  74. }
  75. func setBarPlayer(ncplayer: NCPlayer) {
  76. self.ncplayer = ncplayer
  77. if let durationTime = NCManageDatabase.shared.getVideoDurationTime(metadata: ncplayer.metadata) {
  78. playbackSlider.value = 0
  79. playbackSlider.minimumValue = 0
  80. playbackSlider.maximumValue = Float(durationTime.value)
  81. playbackSlider.addTarget(self, action: #selector(onSliderValChanged(slider:event:)), for: .valueChanged)
  82. labelCurrentTime.text = NCUtility.shared.stringFromTime(.zero)
  83. labelOverallDuration.text = "-" + NCUtility.shared.stringFromTime(durationTime)
  84. }
  85. updateToolBar()
  86. ncplayer.player?.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, preferredTimescale: 1), queue: .main, using: { (CMTime) in
  87. if ncplayer.player?.currentItem?.status == .readyToPlay {
  88. if self.isHidden == false {
  89. self.updateToolBar()
  90. }
  91. }
  92. })
  93. }
  94. @objc public func hideToolBar() {
  95. updateToolBar()
  96. UIView.animate(withDuration: 0.3, animations: {
  97. self.alpha = 0
  98. }, completion: { (value: Bool) in
  99. self.isHidden = true
  100. })
  101. }
  102. @discardableResult
  103. @objc public func showToolBar(metadata: tableMetadata, detailView: NCViewerMediaDetailView?) -> Bool {
  104. if !self.isHidden { return false}
  105. if metadata.livePhoto { return false}
  106. if let detailView = detailView {
  107. if detailView.isShow() { return false }
  108. }
  109. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  110. updateToolBar()
  111. UIView.animate(withDuration: 0.3, animations: {
  112. self.alpha = 1
  113. }, completion: { (value: Bool) in
  114. self.isHidden = false
  115. })
  116. return true
  117. } else {
  118. return false
  119. }
  120. }
  121. public func updateToolBar() {
  122. var namedPlay = "play.fill"
  123. let durationTime = NCManageDatabase.shared.getVideoDurationTime(metadata: ncplayer?.metadata) ?? .zero
  124. let currentTime = ncplayer?.player?.currentTime() ?? .zero
  125. if ncplayer?.player?.rate == 1 { namedPlay = "pause.fill"}
  126. playbackSlider.value = Float(currentTime.value)
  127. playbackSlider.isEnabled = true
  128. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.15", color: .white), for: .normal)
  129. backButton.isEnabled = true
  130. if #available(iOS 13.0, *) {
  131. playButton.setImage(NCUtility.shared.loadImage(named: namedPlay, color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  132. } else {
  133. playButton.setImage(NCUtility.shared.loadImage(named: namedPlay, color: .white), for: .normal)
  134. }
  135. playButton.isEnabled = true
  136. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.15", color: .white), for: .normal)
  137. forwardButton.isEnabled = true
  138. if CCUtility.getAudioMute() {
  139. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .white), for: .normal)
  140. } else {
  141. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
  142. }
  143. muteButton.isEnabled = true
  144. labelCurrentTime.text = NCUtility.shared.stringFromTime(currentTime)
  145. labelOverallDuration.text = "-" + NCUtility.shared.stringFromTime(durationTime - currentTime)
  146. }
  147. //MARK: - Event / Gesture
  148. @objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
  149. if let touchEvent = event.allTouches?.first {
  150. let seconds: Int64 = Int64(self.playbackSlider.value)
  151. let targetTime: CMTime = CMTimeMake(value: seconds, timescale: 1)
  152. switch touchEvent.phase {
  153. case .began:
  154. wasInPlay = ncplayer?.player?.rate == 1 ? true : false
  155. ncplayer?.videoPause()
  156. playbackSliderEvent = .began
  157. case .moved:
  158. ncplayer?.videoSeek(time: targetTime)
  159. playbackSliderEvent = .moved
  160. case .ended:
  161. ncplayer?.videoSeek(time: targetTime)
  162. if wasInPlay {
  163. ncplayer?.videoPlay()
  164. }
  165. playbackSliderEvent = .ended
  166. default:
  167. break
  168. }
  169. }
  170. }
  171. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  172. hideToolBar()
  173. }
  174. //MARK: - Action
  175. @IBAction func buttonTouchInside(_ sender: UIButton) {
  176. hideToolBar()
  177. }
  178. @IBAction func playerPause(_ sender: Any) {
  179. if ncplayer?.player?.timeControlStatus == .playing {
  180. ncplayer?.videoPause()
  181. if let time = ncplayer?.player?.currentTime() {
  182. ncplayer?.saveTime(time)
  183. }
  184. } else if ncplayer?.player?.timeControlStatus == .paused {
  185. ncplayer?.videoPlay()
  186. } else if ncplayer?.player?.timeControlStatus == .waitingToPlayAtSpecifiedRate {
  187. print("timeControlStatus.waitingToPlayAtSpecifiedRate")
  188. if let reason = ncplayer?.player?.reasonForWaitingToPlay {
  189. switch reason {
  190. case .evaluatingBufferingRate:
  191. print("reasonForWaitingToPlay.evaluatingBufferingRate")
  192. case .toMinimizeStalls:
  193. print("reasonForWaitingToPlay.toMinimizeStalls")
  194. case .noItemToPlay:
  195. print("reasonForWaitingToPlay.noItemToPlay")
  196. default:
  197. print("Unknown \(reason)")
  198. }
  199. }
  200. }
  201. }
  202. @IBAction func setMute(_ sender: Any) {
  203. let mute = CCUtility.getAudioMute()
  204. CCUtility.setAudioMute(!mute)
  205. ncplayer?.player?.isMuted = !mute
  206. updateToolBar()
  207. }
  208. @IBAction func forwardButtonSec(_ sender: Any) {
  209. guard let ncplayer = ncplayer else { return }
  210. guard let player = ncplayer.player else { return }
  211. // let playerCurrentTime = CMTimeGetSeconds(player.currentTime())
  212. // let newTime = playerCurrentTime + seekDuration
  213. // if newTime < ncplayer.getVideoDurationSeconds() {
  214. // let time: CMTime = CMTimeMake(value: Int64(newTime * 1000 as Float64), timescale: 1000)
  215. // ncplayer.videoSeek(time: time)
  216. // } else if newTime >= ncplayer.getVideoDurationSeconds() {
  217. // ncplayer.videoSeek(time: .zero)
  218. // }
  219. }
  220. @IBAction func backButtonSec(_ sender: Any) {
  221. guard let ncplayer = ncplayer else { return }
  222. guard let player = ncplayer.player else { return }
  223. // let playerCurrenTime = CMTimeGetSeconds(player.currentTime())
  224. // var newTime = playerCurrenTime - seekDuration
  225. // if newTime < 0 { newTime = 0 }
  226. // let time: CMTime = CMTimeMake(value: Int64(newTime * 1000 as Float64), timescale: 1000)
  227. // ncplayer.videoSeek(time: time)
  228. }
  229. }