NCPlayerToolBar.swift 11 KB

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