NCPlayerToolBar.swift 13 KB

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