NCPlayerToolBar.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. import AVKit
  28. import MediaPlayer
  29. class NCPlayerToolBar: UIView {
  30. @IBOutlet weak var playerTopToolBarView: UIView!
  31. @IBOutlet weak var pipButton: UIButton!
  32. @IBOutlet weak var muteButton: UIButton!
  33. @IBOutlet weak var playButton: UIButton!
  34. @IBOutlet weak var forwardButton: UIButton!
  35. @IBOutlet weak var backButton: UIButton!
  36. @IBOutlet weak var playbackSlider: UISlider!
  37. @IBOutlet weak var labelOverallDuration: UILabel!
  38. @IBOutlet weak var labelCurrentTime: UILabel!
  39. enum sliderEventType {
  40. case began
  41. case ended
  42. case moved
  43. }
  44. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  45. private var ncplayer: NCPlayer?
  46. private var wasInPlay: Bool = false
  47. private var playbackSliderEvent: sliderEventType = .ended
  48. private var timerAutoHide: Timer?
  49. private var metadata: tableMetadata?
  50. private var image: UIImage?
  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. pipButton.isEnabled = false
  75. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .lightGray), for: .normal)
  76. muteButton.isEnabled = false
  77. playbackSlider.value = 0
  78. playbackSlider.minimumValue = 0
  79. playbackSlider.maximumValue = 0
  80. playbackSlider.isContinuous = true
  81. playbackSlider.tintColor = .lightGray
  82. playbackSlider.isEnabled = false
  83. labelCurrentTime.text = NCUtility.shared.stringFromTime(.zero)
  84. labelCurrentTime.textColor = .lightGray
  85. labelOverallDuration.text = NCUtility.shared.stringFromTime(.zero)
  86. labelOverallDuration.textColor = .lightGray
  87. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.10", color: .lightGray), for: .normal)
  88. backButton.isEnabled = false
  89. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .lightGray), for: .normal)
  90. playButton.isEnabled = false
  91. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.10", color: .lightGray), for: .normal)
  92. forwardButton.isEnabled = false
  93. NotificationCenter.default.addObserver(self, selector: #selector(handleInterruption), name: AVAudioSession.interruptionNotification, object: nil)
  94. NotificationCenter.default.addObserver(self, selector: #selector(handleRouteChange), name: AVAudioSession.routeChangeNotification, object: nil)
  95. }
  96. deinit {
  97. print("deinit NCPlayerToolBar")
  98. NotificationCenter.default.removeObserver(self, name: AVAudioSession.interruptionNotification, object: nil)
  99. NotificationCenter.default.removeObserver(self, name: AVAudioSession.routeChangeNotification, object: nil)
  100. }
  101. // MARK: -
  102. func setBarPlayer(ncplayer: NCPlayer, timeSeek: CMTime,metadata: tableMetadata, image: UIImage?) {
  103. self.ncplayer = ncplayer
  104. self.metadata = metadata
  105. self.image = image
  106. playbackSlider.value = 0
  107. playbackSlider.minimumValue = 0
  108. playbackSlider.maximumValue = Float(ncplayer.durationTime.value)
  109. playbackSlider.addTarget(self, action: #selector(onSliderValChanged(slider:event:)), for: .valueChanged)
  110. labelCurrentTime.text = NCUtility.shared.stringFromTime(.zero)
  111. labelOverallDuration.text = "-" + NCUtility.shared.stringFromTime(ncplayer.durationTime)
  112. updateToolBar(commandCenter: true)
  113. }
  114. public func updateToolBar(commandCenter: Bool = false) {
  115. guard let metadata = self.metadata else { return }
  116. var currentTime = appDelegate.player?.currentTime() ?? .zero
  117. currentTime = currentTime.convertScale(1000, method: .default)
  118. // COMMAND CENTER
  119. if commandCenter && CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  120. enableCommandCenter()
  121. }
  122. // MUTE
  123. if CCUtility.getAudioMute() {
  124. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .white), for: .normal)
  125. } else {
  126. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
  127. }
  128. muteButton.isEnabled = true
  129. // PIP
  130. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue && CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  131. pipButton.setImage(NCUtility.shared.loadImage(named: "pip.enter", color: .white), for: .normal)
  132. pipButton.isEnabled = true
  133. if let ncplayer = self.ncplayer, let playerLayer = ncplayer.videoLayer, ncplayer.pictureInPictureController == nil {
  134. ncplayer.pictureInPictureController = AVPictureInPictureController(playerLayer: playerLayer)
  135. ncplayer.pictureInPictureController?.delegate = ncplayer
  136. }
  137. } else {
  138. pipButton.setImage(NCUtility.shared.loadImage(named: "pip.enter", color: .gray), for: .normal)
  139. pipButton.isEnabled = false
  140. if let ncplayer = self.ncplayer, ncplayer.pictureInPictureController != nil {
  141. ncplayer.pictureInPictureController = nil
  142. ncplayer.pictureInPictureController?.delegate = nil
  143. }
  144. }
  145. // SLIDER TIME (START - END)
  146. playbackSlider.value = Float(currentTime.value)
  147. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = currentTime.seconds
  148. playbackSlider.isEnabled = true
  149. labelCurrentTime.text = NCUtility.shared.stringFromTime(currentTime)
  150. // BACK
  151. if #available(iOS 13.0, *) {
  152. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.10", color: .white), for: .normal)
  153. } else {
  154. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.10", color: .white, size: 30), for: .normal)
  155. }
  156. backButton.isEnabled = true
  157. // PLAY
  158. if let ncplayer = ncplayer, ncplayer.isPlay() {
  159. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 1
  160. } else {
  161. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0
  162. }
  163. let namedPlay = (ncplayer?.isPlay() ?? false) ? "pause.fill" : "play.fill"
  164. if #available(iOS 13.0, *) {
  165. playButton.setImage(NCUtility.shared.loadImage(named: namedPlay, color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  166. } else {
  167. playButton.setImage(NCUtility.shared.loadImage(named: namedPlay, color: .white, size: 30), for: .normal)
  168. }
  169. playButton.isEnabled = true
  170. // FORWARD
  171. if #available(iOS 13.0, *) {
  172. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.10", color: .white), for: .normal)
  173. } else {
  174. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.10", color: .white, size: 30), for: .normal)
  175. }
  176. forwardButton.isEnabled = true
  177. }
  178. // MARK: - Command Center
  179. func enableCommandCenter() {
  180. guard let ncplayer = self.ncplayer else { return }
  181. UIApplication.shared.beginReceivingRemoteControlEvents()
  182. var nowPlayingInfo = [String : Any]()
  183. // Add handler for Play Command
  184. appDelegate.commandCenterPlayCommand = MPRemoteCommandCenter.shared().playCommand.addTarget { event in
  185. if !ncplayer.isPlay() {
  186. ncplayer.playerPlay()
  187. return .success
  188. }
  189. return .commandFailed
  190. }
  191. // Add handler for Pause Command
  192. appDelegate.commandCenterPauseCommand = MPRemoteCommandCenter.shared().pauseCommand.addTarget { event in
  193. if ncplayer.isPlay() {
  194. ncplayer.playerPause()
  195. return .success
  196. }
  197. return .commandFailed
  198. }
  199. // Add handler for Backward Command
  200. appDelegate.commandCenterSkipBackwardCommand = MPRemoteCommandCenter.shared().skipBackwardCommand.addTarget { event in
  201. let seconds = Float64((event as! MPSkipIntervalCommandEvent).interval)
  202. self.skip(seconds: -seconds)
  203. return.success
  204. }
  205. // Add handler for Forward Command
  206. appDelegate.commandCenterSkipForwardCommand = MPRemoteCommandCenter.shared().skipForwardCommand.addTarget { event in
  207. let seconds = Float64((event as! MPSkipIntervalCommandEvent).interval)
  208. self.skip(seconds: seconds)
  209. return.success
  210. }
  211. nowPlayingInfo[MPMediaItemPropertyTitle] = metadata?.fileNameView
  212. nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = appDelegate.player?.currentItem?.asset.duration.seconds
  213. if let image = self.image {
  214. nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { size in
  215. return image
  216. }
  217. }
  218. MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
  219. }
  220. func disableCommandCenter() {
  221. UIApplication.shared.endReceivingRemoteControlEvents()
  222. MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
  223. if let playCommand = appDelegate.commandCenterPlayCommand {
  224. MPRemoteCommandCenter.shared().playCommand.removeTarget(playCommand)
  225. appDelegate.commandCenterPlayCommand = nil
  226. }
  227. if let pauseCommand = appDelegate.commandCenterPauseCommand {
  228. MPRemoteCommandCenter.shared().pauseCommand.removeTarget(pauseCommand)
  229. appDelegate.commandCenterPauseCommand = nil
  230. }
  231. if let commandCenterSkipBackwardCommand = appDelegate.commandCenterSkipBackwardCommand {
  232. MPRemoteCommandCenter.shared().previousTrackCommand.removeTarget(commandCenterSkipBackwardCommand)
  233. appDelegate.commandCenterSkipBackwardCommand = nil
  234. }
  235. if let commandCenterSkipForwardCommand = appDelegate.commandCenterSkipForwardCommand {
  236. MPRemoteCommandCenter.shared().nextTrackCommand.removeTarget(commandCenterSkipForwardCommand)
  237. appDelegate.commandCenterSkipForwardCommand = nil
  238. }
  239. }
  240. // MARK: Handle Notifications
  241. @objc func handleRouteChange(notification: Notification) {
  242. guard let userInfo = notification.userInfo, let reasonValue = userInfo[AVAudioSessionRouteChangeReasonKey] as? UInt, let reason = AVAudioSession.RouteChangeReason(rawValue:reasonValue) else { return }
  243. switch reason {
  244. case .newDeviceAvailable:
  245. let session = AVAudioSession.sharedInstance()
  246. for output in session.currentRoute.outputs where output.portType == AVAudioSession.Port.headphones {
  247. print("headphones connected")
  248. DispatchQueue.main.sync {
  249. ncplayer?.playerPlay()
  250. startTimerAutoHide()
  251. }
  252. break
  253. }
  254. case .oldDeviceUnavailable:
  255. if let previousRoute = userInfo[AVAudioSessionRouteChangePreviousRouteKey] as? AVAudioSessionRouteDescription {
  256. for output in previousRoute.outputs where output.portType == AVAudioSession.Port.headphones {
  257. print("headphones disconnected")
  258. DispatchQueue.main.sync {
  259. ncplayer?.playerPause()
  260. ncplayer?.saveCurrentTime()
  261. }
  262. break
  263. }
  264. }
  265. default: ()
  266. }
  267. }
  268. @objc func handleInterruption(notification: Notification) {
  269. guard let userInfo = notification.userInfo, let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt, let type = AVAudioSession.InterruptionType(rawValue: typeValue) else { return }
  270. if type == .began {
  271. print("Interruption began")
  272. } else if type == .ended {
  273. if let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt {
  274. let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue)
  275. if options.contains(.shouldResume) {
  276. print("Interruption Ended - playback should resume")
  277. ncplayer?.playerPlay()
  278. startTimerAutoHide()
  279. } else {
  280. print("Interruption Ended - playback should NOT resume")
  281. }
  282. }
  283. }
  284. }
  285. // MARK: -
  286. public func show(enableTimerAutoHide: Bool = false) {
  287. guard let metadata = self.metadata else { return }
  288. if metadata.classFile != NCCommunicationCommon.typeClassFile.video.rawValue && metadata.classFile != NCCommunicationCommon.typeClassFile.audio.rawValue { return }
  289. if metadata.livePhoto { return }
  290. timerAutoHide?.invalidate()
  291. if enableTimerAutoHide {
  292. startTimerAutoHide()
  293. }
  294. if !self.isHidden { return }
  295. UIView.animate(withDuration: 0.3, animations: {
  296. self.alpha = 1
  297. self.playerTopToolBarView.alpha = 1
  298. }, completion: { (value: Bool) in
  299. self.isHidden = false
  300. self.playerTopToolBarView.isHidden = false
  301. })
  302. }
  303. func isShow() -> Bool {
  304. return !self.isHidden
  305. }
  306. public func hide() {
  307. UIView.animate(withDuration: 0.3, animations: {
  308. self.alpha = 0
  309. self.playerTopToolBarView.alpha = 0
  310. }, completion: { (value: Bool) in
  311. self.isHidden = true
  312. self.playerTopToolBarView.isHidden = true
  313. })
  314. }
  315. @objc private func automaticHide() {
  316. if let metadata = self.metadata {
  317. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterHidePlayerToolBar, userInfo: ["ocId":metadata.ocId])
  318. }
  319. }
  320. private func startTimerAutoHide() {
  321. timerAutoHide?.invalidate()
  322. timerAutoHide = Timer.scheduledTimer(timeInterval: 3.5, target: self, selector: #selector(automaticHide), userInfo: nil, repeats: false)
  323. }
  324. private func reStartTimerAutoHide() {
  325. if let timerAutoHide = timerAutoHide, timerAutoHide.isValid {
  326. startTimerAutoHide()
  327. }
  328. }
  329. func skip(seconds: Float64) {
  330. guard let ncplayer = ncplayer else { return }
  331. guard let player = appDelegate.player else { return }
  332. let currentTime = player.currentTime()
  333. var newTime: CMTime = .zero
  334. let timeToAdd: CMTime = CMTimeMakeWithSeconds(abs(seconds), preferredTimescale: 1)
  335. if seconds > 0 {
  336. newTime = CMTimeAdd(currentTime, timeToAdd)
  337. if newTime < ncplayer.durationTime {
  338. ncplayer.videoSeek(time: newTime)
  339. } else if newTime >= ncplayer.durationTime {
  340. let timeToSubtract: CMTime = CMTimeMakeWithSeconds(3, preferredTimescale: 1)
  341. newTime = CMTimeSubtract(ncplayer.durationTime, timeToSubtract)
  342. if newTime > currentTime {
  343. ncplayer.videoSeek(time: newTime)
  344. }
  345. }
  346. } else {
  347. newTime = CMTimeSubtract(currentTime, timeToAdd)
  348. ncplayer.videoSeek(time: newTime)
  349. }
  350. reStartTimerAutoHide()
  351. }
  352. //MARK: - Event / Gesture
  353. @objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
  354. if let touchEvent = event.allTouches?.first, let ncplayer = ncplayer {
  355. let seconds: Int64 = Int64(self.playbackSlider.value)
  356. let targetTime: CMTime = CMTimeMake(value: seconds, timescale: 1000)
  357. switch touchEvent.phase {
  358. case .began:
  359. wasInPlay = ncplayer.isPlay()
  360. ncplayer.playerPause()
  361. playbackSliderEvent = .began
  362. case .moved:
  363. ncplayer.videoSeek(time: targetTime)
  364. playbackSliderEvent = .moved
  365. case .ended:
  366. ncplayer.videoSeek(time: targetTime)
  367. if wasInPlay {
  368. ncplayer.playerPlay()
  369. }
  370. playbackSliderEvent = .ended
  371. default:
  372. break
  373. }
  374. reStartTimerAutoHide()
  375. }
  376. }
  377. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  378. hide()
  379. }
  380. //MARK: - Action
  381. @IBAction func buttonTouchInside(_ sender: UIButton) {
  382. }
  383. @IBAction func playerPause(_ sender: Any) {
  384. if appDelegate.player?.timeControlStatus == .playing {
  385. ncplayer?.playerPause()
  386. ncplayer?.saveCurrentTime()
  387. timerAutoHide?.invalidate()
  388. } else if appDelegate.player?.timeControlStatus == .paused {
  389. ncplayer?.playerPlay()
  390. startTimerAutoHide()
  391. } else if appDelegate.player?.timeControlStatus == .waitingToPlayAtSpecifiedRate {
  392. print("timeControlStatus.waitingToPlayAtSpecifiedRate")
  393. if let reason = appDelegate.player?.reasonForWaitingToPlay {
  394. switch reason {
  395. case .evaluatingBufferingRate:
  396. print("reasonForWaitingToPlay.evaluatingBufferingRate")
  397. case .toMinimizeStalls:
  398. print("reasonForWaitingToPlay.toMinimizeStalls")
  399. case .noItemToPlay:
  400. print("reasonForWaitingToPlay.noItemToPlay")
  401. default:
  402. print("Unknown \(reason)")
  403. }
  404. }
  405. }
  406. }
  407. @IBAction func setMute(_ sender: Any) {
  408. let mute = CCUtility.getAudioMute()
  409. CCUtility.setAudioMute(!mute)
  410. appDelegate.player?.isMuted = !mute
  411. updateToolBar()
  412. reStartTimerAutoHide()
  413. }
  414. @IBAction func setPip(_ sender: Any) {
  415. guard let metadata = self.metadata else { return }
  416. ncplayer?.pictureInPictureController?.startPictureInPicture()
  417. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterHidePlayerToolBar, userInfo: ["ocId":metadata.ocId])
  418. }
  419. @IBAction func forwardButtonSec(_ sender: Any) {
  420. skip(seconds: 10)
  421. }
  422. @IBAction func backButtonSec(_ sender: Any) {
  423. skip(seconds: -10)
  424. }
  425. }