NCPlayerToolBar.swift 11 KB

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