NCPlayerToolBar.swift 12 KB

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