NCPlayerToolBar.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 NextcloudKit
  25. import CoreMedia
  26. import UIKit
  27. import AVKit
  28. import MediaPlayer
  29. import MobileVLCKit
  30. class NCPlayerToolBar: UIView {
  31. @IBOutlet weak var playerTopToolBarView: UIStackView!
  32. @IBOutlet weak var playerToolBarView: UIView!
  33. @IBOutlet weak var muteButton: UIButton!
  34. @IBOutlet weak var playButton: UIButton!
  35. @IBOutlet weak var forwardButton: UIButton!
  36. @IBOutlet weak var backButton: UIButton!
  37. @IBOutlet weak var playbackSlider: UISlider!
  38. @IBOutlet weak var labelLeftTime: UILabel!
  39. @IBOutlet weak var labelCurrentTime: UILabel!
  40. enum sliderEventType {
  41. case began
  42. case ended
  43. case moved
  44. }
  45. var ncplayer: NCPlayer?
  46. private var metadata: tableMetadata?
  47. private var wasInPlay: Bool = false
  48. private var playbackSliderEvent: sliderEventType = .ended
  49. private var timerAutoHide: Timer?
  50. private var timerAutoHideSeconds: Double {
  51. get {
  52. if NCUtility.shared.isSimulator() { // for test
  53. return 15
  54. } else {
  55. return 3.5
  56. }
  57. }
  58. }
  59. weak var viewerMediaPage: NCViewerMediaPage?
  60. // MARK: - View Life Cycle
  61. override func awakeFromNib() {
  62. super.awakeFromNib()
  63. let blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
  64. blurEffectView.frame = self.bounds
  65. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  66. playerToolBarView.insertSubview(blurEffectView, at: 0)
  67. playerTopToolBarView.layer.cornerRadius = 10
  68. playerTopToolBarView.layer.masksToBounds = true
  69. playerTopToolBarView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapTopToolBarWith(gestureRecognizer:))))
  70. let blurEffectTopToolBarView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
  71. blurEffectTopToolBarView.frame = playerTopToolBarView.bounds
  72. blurEffectTopToolBarView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  73. playerTopToolBarView.insertSubview(blurEffectTopToolBarView, at: 0)
  74. playerToolBarView.layer.cornerRadius = 10
  75. playerToolBarView.layer.masksToBounds = true
  76. playerToolBarView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapToolBarWith(gestureRecognizer:))))
  77. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .lightGray), for: .normal)
  78. muteButton.isEnabled = false
  79. playbackSlider.value = 0
  80. playbackSlider.minimumValue = 0
  81. playbackSlider.maximumValue = 1
  82. playbackSlider.isContinuous = true
  83. playbackSlider.tintColor = .lightGray
  84. playbackSlider.isEnabled = false
  85. labelCurrentTime.text = NCUtility.shared.stringFromTime(.zero)
  86. labelCurrentTime.textColor = .lightGray
  87. labelLeftTime.text = NCUtility.shared.stringFromTime(.zero)
  88. labelLeftTime.textColor = .lightGray
  89. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.10", color: .lightGray), for: .normal)
  90. backButton.isEnabled = false
  91. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .lightGray, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  92. playButton.isEnabled = false
  93. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.10", color: .lightGray), for: .normal)
  94. forwardButton.isEnabled = false
  95. }
  96. required init?(coder aDecoder: NSCoder) {
  97. super.init(coder: aDecoder)
  98. }
  99. deinit {
  100. print("deinit NCPlayerToolBar")
  101. }
  102. // MARK: -
  103. func setMetadata(_ metadata: tableMetadata) {
  104. self.metadata = metadata
  105. }
  106. func setBarPlayer(ncplayer: NCPlayer, position: Float) {
  107. self.ncplayer = ncplayer
  108. playbackSlider.value = position
  109. playbackSlider.addTarget(self, action: #selector(onSliderValChanged(slider:event:)), for: .valueChanged)
  110. labelCurrentTime.text = ncplayer.player?.time.stringValue
  111. labelLeftTime.text = ncplayer.player?.remainingTime?.stringValue
  112. update(position: position)
  113. }
  114. public func buffering() {
  115. muteButton.isEnabled = false
  116. playButton.isEnabled = false
  117. forwardButton.isEnabled = false
  118. backButton.isEnabled = false
  119. playbackSlider.isEnabled = false
  120. }
  121. public func update(position: Float?) {
  122. guard let ncplayer = self.ncplayer,
  123. let length = ncplayer.player?.media?.length.intValue,
  124. let position = position
  125. else { return }
  126. // SAVE POSITION
  127. if position > 0 {
  128. ncplayer.savePosition(position)
  129. }
  130. // MUTE
  131. if let muteButton = muteButton {
  132. let audio = CCUtility.getAudioVolume()
  133. if audio == 0 {
  134. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .white), for: .normal)
  135. } else {
  136. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
  137. }
  138. muteButton.isEnabled = true
  139. }
  140. // SLIDER TIME (START - END)
  141. playbackSlider.value = position
  142. playbackSlider.isEnabled = true
  143. labelCurrentTime.text = ncplayer.player?.time.stringValue
  144. labelLeftTime.text = ncplayer.player?.remainingTime?.stringValue
  145. // BACK FORWARD
  146. if length > 0 {
  147. backButton.isEnabled = true
  148. forwardButton.isEnabled = true
  149. } else {
  150. backButton.isEnabled = false
  151. forwardButton.isEnabled = false
  152. }
  153. // PLAY
  154. if ncplayer.isPlay() {
  155. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 1
  156. } else {
  157. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0
  158. }
  159. let namedPlay = ncplayer.isPlay() ? "pause.fill" : "play.fill"
  160. playButton.setImage(NCUtility.shared.loadImage(named: namedPlay, color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  161. playButton.isEnabled = true
  162. }
  163. // MARK: -
  164. public func show(enableTimerAutoHide: Bool = false) {
  165. guard let metadata = self.metadata, ncplayer != nil, !metadata.livePhoto else { return }
  166. if metadata.classFile != NKCommon.TypeClassFile.video.rawValue && metadata.classFile != NKCommon.TypeClassFile.audio.rawValue { return }
  167. timerAutoHide?.invalidate()
  168. if enableTimerAutoHide {
  169. startTimerAutoHide()
  170. }
  171. if !self.isHidden { return }
  172. UIView.animate(withDuration: 0.3, animations: {
  173. self.alpha = 1
  174. }, completion: { (_: Bool) in
  175. self.isHidden = false
  176. })
  177. update(position: ncplayer?.player?.position)
  178. }
  179. func isShow() -> Bool {
  180. return !self.isHidden
  181. }
  182. public func hide() {
  183. UIView.animate(withDuration: 0.3, animations: {
  184. self.alpha = 0
  185. }, completion: { (_: Bool) in
  186. self.isHidden = true
  187. })
  188. }
  189. @objc private func automaticHide() {
  190. if let metadata = self.metadata {
  191. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterHidePlayerToolBar, userInfo: ["ocId": metadata.ocId])
  192. }
  193. }
  194. private func startTimerAutoHide() {
  195. timerAutoHide?.invalidate()
  196. timerAutoHide = Timer.scheduledTimer(timeInterval: timerAutoHideSeconds, target: self, selector: #selector(automaticHide), userInfo: nil, repeats: false)
  197. }
  198. private func reStartTimerAutoHide() {
  199. if let timerAutoHide = timerAutoHide, timerAutoHide.isValid {
  200. startTimerAutoHide()
  201. }
  202. }
  203. func skip(seconds: Float) {
  204. guard let ncplayer = ncplayer,
  205. let player = ncplayer.player,
  206. let lengthInSeconds = player.media?.length.intValue
  207. else { return }
  208. let currentInSeconds = player.position * Float(lengthInSeconds / 1000)
  209. let newPositionSeconds = currentInSeconds + seconds
  210. let newPosition = newPositionSeconds / Float(lengthInSeconds / 1000)
  211. ncplayer.videoSeek(position: newPosition)
  212. update(position: newPosition)
  213. reStartTimerAutoHide()
  214. }
  215. func stopTimerAutoHide() {
  216. timerAutoHide?.invalidate()
  217. }
  218. // MARK: - Event / Gesture
  219. @objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
  220. guard let touchEvent = event.allTouches?.first,
  221. let ncplayer = ncplayer
  222. else { return }
  223. let newPosition = self.playbackSlider.value
  224. switch touchEvent.phase {
  225. case .began:
  226. ncplayer.playerPause()
  227. playbackSliderEvent = .began
  228. case .moved:
  229. playbackSliderEvent = .moved
  230. case .ended:
  231. ncplayer.playerPlay()
  232. ncplayer.videoSeek(position: newPosition)
  233. playbackSliderEvent = .ended
  234. default:
  235. break
  236. }
  237. reStartTimerAutoHide()
  238. }
  239. // MARK: - Action
  240. @objc func tapTopToolBarWith(gestureRecognizer: UITapGestureRecognizer) { }
  241. @objc func tapToolBarWith(gestureRecognizer: UITapGestureRecognizer) { }
  242. @IBAction func tapPlayerPause(_ sender: Any) {
  243. guard let ncplayer = ncplayer else { return }
  244. if ncplayer.isPlay() {
  245. ncplayer.playerPause()
  246. timerAutoHide?.invalidate()
  247. } else {
  248. ncplayer.playerPlay()
  249. startTimerAutoHide()
  250. }
  251. }
  252. @IBAction func tapMute(_ sender: Any) {
  253. let volume = CCUtility.getAudioVolume()
  254. if volume > 0 {
  255. CCUtility.setAudioVolume(0)
  256. ncplayer?.player?.audio?.volume = 0
  257. } else {
  258. CCUtility.setAudioVolume(100)
  259. ncplayer?.player?.audio?.volume = 100
  260. }
  261. update(position: ncplayer?.player?.position)
  262. reStartTimerAutoHide()
  263. }
  264. @IBAction func tapForward(_ sender: Any) {
  265. skip(seconds: 10)
  266. }
  267. @IBAction func tapBack(_ sender: Any) {
  268. skip(seconds: -10)
  269. }
  270. func forward() {
  271. var index: Int = 0
  272. if let currentIndex = self.viewerMediaPage?.currentIndex, let metadatas = self.viewerMediaPage?.metadatas, let ncplayer = self.ncplayer {
  273. if currentIndex == metadatas.count - 1 {
  274. index = 0
  275. } else {
  276. index = currentIndex + 1
  277. }
  278. self.viewerMediaPage?.goTo(index: index, direction: .forward, autoPlay: ncplayer.isPlay())
  279. }
  280. }
  281. func backward() {
  282. var index: Int = 0
  283. if let currentIndex = self.viewerMediaPage?.currentIndex, let metadatas = self.viewerMediaPage?.metadatas, let ncplayer = self.ncplayer {
  284. if currentIndex == 0 {
  285. index = metadatas.count - 1
  286. } else {
  287. index = currentIndex - 1
  288. }
  289. self.viewerMediaPage?.goTo(index: index, direction: .reverse, autoPlay: ncplayer.isPlay())
  290. }
  291. }
  292. }
  293. extension NCPlayerToolBar: AVPictureInPictureControllerDelegate {
  294. func pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
  295. if let metadata = self.metadata, let ncplayer = self.ncplayer, !ncplayer.isPlay() {
  296. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId": metadata.ocId, "enableTimerAutoHide": false])
  297. }
  298. }
  299. }