NCPlayerToolBar.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 durationTime: CMTime = .zero
  49. private var timeObserver: Any?
  50. private var timerAutoHide: Timer?
  51. private var metadata: tableMetadata?
  52. private var image: UIImage?
  53. // MARK: - View Life Cycle
  54. override func awakeFromNib() {
  55. super.awakeFromNib()
  56. // for disable gesture of UIPageViewController
  57. let panRecognizer = UIPanGestureRecognizer(target: self, action: nil)
  58. addGestureRecognizer(panRecognizer)
  59. let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSingleTapWith(gestureRecognizer:)))
  60. addGestureRecognizer(singleTapGestureRecognizer)
  61. // self
  62. self.layer.cornerRadius = 15
  63. self.layer.masksToBounds = true
  64. let blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
  65. blurEffectView.frame = self.bounds
  66. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  67. self.insertSubview(blurEffectView, at:0)
  68. // Top ToolBar
  69. playerTopToolBarView.layer.cornerRadius = 10
  70. playerTopToolBarView.layer.masksToBounds = true
  71. let blurEffectTopToolBarView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
  72. blurEffectTopToolBarView.frame = playerTopToolBarView.bounds
  73. blurEffectTopToolBarView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  74. playerTopToolBarView.insertSubview(blurEffectTopToolBarView, at:0)
  75. pipButton.setImage(NCUtility.shared.loadImage(named: "pip.enter", color: .lightGray), for: .normal)
  76. pipButton.isEnabled = false
  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 = 0
  82. playbackSlider.isContinuous = true
  83. playbackSlider.tintColor = .lightGray
  84. playbackSlider.isEnabled = false
  85. labelCurrentTime.text = NCUtility.shared.stringFromTime(.zero)
  86. labelCurrentTime.textColor = .lightGray
  87. labelOverallDuration.text = NCUtility.shared.stringFromTime(.zero)
  88. labelOverallDuration.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), for: .normal)
  92. playButton.isEnabled = false
  93. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.10", color: .lightGray), for: .normal)
  94. forwardButton.isEnabled = false
  95. NotificationCenter.default.addObserver(self, selector: #selector(handleInterruption), name: AVAudioSession.interruptionNotification, object: nil)
  96. NotificationCenter.default.addObserver(self, selector: #selector(handleRouteChange), name: AVAudioSession.routeChangeNotification, object: nil)
  97. }
  98. deinit {
  99. print("deinit NCPlayerToolBar")
  100. if self.timeObserver != nil {
  101. appDelegate.player?.removeTimeObserver(self.timeObserver!)
  102. }
  103. deinitCommandCenter()
  104. NotificationCenter.default.removeObserver(self, name: AVAudioSession.interruptionNotification, object: nil)
  105. NotificationCenter.default.removeObserver(self, name: AVAudioSession.routeChangeNotification, object: nil)
  106. }
  107. // MARK: -
  108. func setBarPlayer(ncplayer: NCPlayer, timeSeek: CMTime, metadata: tableMetadata, image: UIImage?) {
  109. self.ncplayer = ncplayer
  110. self.metadata = metadata
  111. self.image = image
  112. if let durationTime = NCManageDatabase.shared.getVideoDurationTime(metadata: ncplayer.metadata) {
  113. self.durationTime = durationTime
  114. playbackSlider.value = 0
  115. playbackSlider.minimumValue = 0
  116. playbackSlider.maximumValue = Float(durationTime.value)
  117. playbackSlider.addTarget(self, action: #selector(onSliderValChanged(slider:event:)), for: .valueChanged)
  118. labelCurrentTime.text = NCUtility.shared.stringFromTime(.zero)
  119. labelOverallDuration.text = "-" + NCUtility.shared.stringFromTime(durationTime)
  120. }
  121. /*
  122. if let ncplayer = self.ncplayer {
  123. UIApplication.shared.beginReceivingRemoteControlEvents()
  124. let commandCenter = MPRemoteCommandCenter.shared()
  125. var nowPlayingInfo = [String : Any]()
  126. commandCenter.playCommand.isEnabled = true
  127. // Add handler for Play Command
  128. appDelegate.commandCenterPlayCommand = commandCenter.playCommand.addTarget { event in
  129. if !ncplayer.isPlay() {
  130. ncplayer.playerPlay()
  131. self.updateToolBar(timeSeek: nil)
  132. return .success
  133. }
  134. return .commandFailed
  135. }
  136. // Add handler for Pause Command
  137. appDelegate.commandCenterPauseCommand = commandCenter.pauseCommand.addTarget { event in
  138. if ncplayer.isPlay() {
  139. ncplayer.playerPause()
  140. self.updateToolBar(timeSeek: nil)
  141. return .success
  142. }
  143. return .commandFailed
  144. }
  145. // Add handler for Backward Command
  146. appDelegate.commandCenterSkipBackwardCommand = commandCenter.skipBackwardCommand.addTarget { event in
  147. let seconds = Float64((event as! MPSkipIntervalCommandEvent).interval)
  148. self.skip(seconds: -seconds)
  149. return.success
  150. }
  151. // Add handler for Forward Command
  152. appDelegate.commandCenterSkipForwardCommand = commandCenter.skipForwardCommand.addTarget { event in
  153. let seconds = Float64((event as! MPSkipIntervalCommandEvent).interval)
  154. self.skip(seconds: seconds)
  155. return.success
  156. }
  157. nowPlayingInfo[MPMediaItemPropertyTitle] = metadata.fileNameView
  158. nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = appDelegate.player?.currentItem?.asset.duration.seconds
  159. if let image = self.image {
  160. nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { size in
  161. return image
  162. }
  163. }
  164. MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
  165. }
  166. */
  167. updateToolBar(timeSeek: timeSeek)
  168. self.timeObserver = appDelegate.player?.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, preferredTimescale: 1), queue: .main, using: { (CMTime) in
  169. if self.appDelegate.player?.currentItem?.status == .readyToPlay {
  170. if self.isHidden == false {
  171. self.updateToolBar()
  172. }
  173. }
  174. })
  175. }
  176. public func updateToolBar(timeSeek: CMTime? = nil) {
  177. guard let metadata = self.metadata else { return }
  178. var currentTime = appDelegate.player?.currentTime() ?? .zero
  179. currentTime = currentTime.convertScale(1000, method: .default)
  180. // MUTE
  181. if CCUtility.getAudioMute() {
  182. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .white), for: .normal)
  183. } else {
  184. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
  185. }
  186. muteButton.isEnabled = true
  187. // PIP
  188. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  189. pipButton.setImage(NCUtility.shared.loadImage(named: "pip.enter", color: .white), for: .normal)
  190. pipButton.isEnabled = true
  191. if let ncplayer = self.ncplayer, let playerLayer = ncplayer.videoLayer, ncplayer.pictureInPictureController == nil {
  192. ncplayer.pictureInPictureController = AVPictureInPictureController(playerLayer: playerLayer)
  193. ncplayer.pictureInPictureController?.delegate = ncplayer
  194. }
  195. } else {
  196. pipButton.setImage(NCUtility.shared.loadImage(named: "pip.enter", color: .gray), for: .normal)
  197. pipButton.isEnabled = false
  198. if let ncplayer = self.ncplayer, ncplayer.pictureInPictureController != nil {
  199. ncplayer.pictureInPictureController = nil
  200. ncplayer.pictureInPictureController?.delegate = nil
  201. }
  202. }
  203. // BACK
  204. if timeSeek != nil {
  205. playbackSlider.value = Float(timeSeek!.value)
  206. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = timeSeek!.seconds
  207. } else {
  208. playbackSlider.value = Float(currentTime.value)
  209. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = currentTime.seconds
  210. }
  211. playbackSlider.isEnabled = true
  212. if #available(iOS 13.0, *) {
  213. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.10", color: .white), for: .normal)
  214. } else {
  215. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.10", color: .white, size: 30), for: .normal)
  216. }
  217. backButton.isEnabled = true
  218. // PLAY
  219. if let ncplayer = ncplayer, ncplayer.isPlay() {
  220. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 1
  221. } else {
  222. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0
  223. }
  224. let namedPlay = (ncplayer?.isPlay() ?? false) ? "pause.fill" : "play.fill"
  225. if #available(iOS 13.0, *) {
  226. playButton.setImage(NCUtility.shared.loadImage(named: namedPlay, color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  227. } else {
  228. playButton.setImage(NCUtility.shared.loadImage(named: namedPlay, color: .white, size: 30), for: .normal)
  229. }
  230. playButton.isEnabled = true
  231. // FORWARD
  232. if #available(iOS 13.0, *) {
  233. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.10", color: .white), for: .normal)
  234. } else {
  235. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.10", color: .white, size: 30), for: .normal)
  236. }
  237. forwardButton.isEnabled = true
  238. // TIME (START - END)
  239. labelCurrentTime.text = NCUtility.shared.stringFromTime(currentTime)
  240. labelOverallDuration.text = "-" + NCUtility.shared.stringFromTime(self.durationTime - currentTime)
  241. // COMMAND CENTER
  242. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  243. initCommandCenter()
  244. } else {
  245. deinitCommandCenter()
  246. }
  247. }
  248. // MARK: - Command Center
  249. func initCommandCenter() {
  250. guard let ncplayer = self.ncplayer else { return }
  251. UIApplication.shared.beginReceivingRemoteControlEvents()
  252. MPRemoteCommandCenter.shared().playCommand.isEnabled = true
  253. var nowPlayingInfo = [String : Any]()
  254. // Add handler for Play Command
  255. appDelegate.commandCenterPlayCommand = MPRemoteCommandCenter.shared().playCommand.addTarget { event in
  256. if !ncplayer.isPlay() {
  257. ncplayer.playerPlay()
  258. self.updateToolBar(timeSeek: nil)
  259. return .success
  260. }
  261. return .commandFailed
  262. }
  263. // Add handler for Pause Command
  264. appDelegate.commandCenterPauseCommand = MPRemoteCommandCenter.shared().pauseCommand.addTarget { event in
  265. if ncplayer.isPlay() {
  266. ncplayer.playerPause()
  267. self.updateToolBar(timeSeek: nil)
  268. return .success
  269. }
  270. return .commandFailed
  271. }
  272. // Add handler for Backward Command
  273. appDelegate.commandCenterSkipBackwardCommand = MPRemoteCommandCenter.shared().skipBackwardCommand.addTarget { event in
  274. let seconds = Float64((event as! MPSkipIntervalCommandEvent).interval)
  275. self.skip(seconds: -seconds)
  276. return.success
  277. }
  278. // Add handler for Forward Command
  279. appDelegate.commandCenterSkipForwardCommand = MPRemoteCommandCenter.shared().skipForwardCommand.addTarget { event in
  280. let seconds = Float64((event as! MPSkipIntervalCommandEvent).interval)
  281. self.skip(seconds: seconds)
  282. return.success
  283. }
  284. nowPlayingInfo[MPMediaItemPropertyTitle] = metadata?.fileNameView
  285. nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = appDelegate.player?.currentItem?.asset.duration.seconds
  286. if let image = self.image {
  287. nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { size in
  288. return image
  289. }
  290. }
  291. MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
  292. }
  293. func deinitCommandCenter() {
  294. UIApplication.shared.endReceivingRemoteControlEvents()
  295. MPRemoteCommandCenter.shared().playCommand.isEnabled = false
  296. MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
  297. if let playCommand = appDelegate.commandCenterPlayCommand {
  298. MPRemoteCommandCenter.shared().playCommand.removeTarget(playCommand)
  299. }
  300. if let pauseCommand = appDelegate.commandCenterPauseCommand {
  301. MPRemoteCommandCenter.shared().pauseCommand.removeTarget(pauseCommand)
  302. appDelegate.commandCenterPauseCommand = nil
  303. }
  304. if let commandCenterSkipBackwardCommand = appDelegate.commandCenterSkipBackwardCommand {
  305. MPRemoteCommandCenter.shared().previousTrackCommand.removeTarget(commandCenterSkipBackwardCommand)
  306. appDelegate.commandCenterSkipBackwardCommand = nil
  307. }
  308. if let commandCenterSkipForwardCommand = appDelegate.commandCenterSkipForwardCommand {
  309. MPRemoteCommandCenter.shared().nextTrackCommand.removeTarget(commandCenterSkipForwardCommand)
  310. appDelegate.commandCenterSkipForwardCommand = nil
  311. }
  312. }
  313. // MARK: Handle Notifications
  314. @objc func handleRouteChange(notification: Notification) {
  315. guard let userInfo = notification.userInfo, let reasonValue = userInfo[AVAudioSessionRouteChangeReasonKey] as? UInt, let reason = AVAudioSession.RouteChangeReason(rawValue:reasonValue) else { return }
  316. switch reason {
  317. case .newDeviceAvailable:
  318. let session = AVAudioSession.sharedInstance()
  319. for output in session.currentRoute.outputs where output.portType == AVAudioSession.Port.headphones {
  320. print("headphones connected")
  321. DispatchQueue.main.sync {
  322. ncplayer?.playerPlay()
  323. startTimerAutoHide()
  324. }
  325. break
  326. }
  327. case .oldDeviceUnavailable:
  328. if let previousRoute = userInfo[AVAudioSessionRouteChangePreviousRouteKey] as? AVAudioSessionRouteDescription {
  329. for output in previousRoute.outputs where output.portType == AVAudioSession.Port.headphones {
  330. print("headphones disconnected")
  331. DispatchQueue.main.sync {
  332. ncplayer?.playerPause()
  333. ncplayer?.saveCurrentTime()
  334. }
  335. break
  336. }
  337. }
  338. default: ()
  339. }
  340. }
  341. @objc func handleInterruption(notification: Notification) {
  342. guard let userInfo = notification.userInfo, let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt, let type = AVAudioSession.InterruptionType(rawValue: typeValue) else { return }
  343. if type == .began {
  344. print("Interruption began")
  345. } else if type == .ended {
  346. if let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt {
  347. let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue)
  348. if options.contains(.shouldResume) {
  349. print("Interruption Ended - playback should resume")
  350. ncplayer?.playerPlay()
  351. startTimerAutoHide()
  352. } else {
  353. print("Interruption Ended - playback should NOT resume")
  354. }
  355. }
  356. }
  357. }
  358. // MARK: -
  359. public func show(enableTimerAutoHide: Bool) {
  360. guard let metadata = self.metadata else { return }
  361. if metadata.classFile != NCCommunicationCommon.typeClassFile.video.rawValue && metadata.classFile != NCCommunicationCommon.typeClassFile.audio.rawValue { return }
  362. if metadata.livePhoto { return }
  363. timerAutoHide?.invalidate()
  364. if enableTimerAutoHide {
  365. startTimerAutoHide()
  366. }
  367. if !self.isHidden { return }
  368. updateToolBar()
  369. UIView.animate(withDuration: 0.3, animations: {
  370. self.alpha = 1
  371. self.playerTopToolBarView.alpha = 1
  372. }, completion: { (value: Bool) in
  373. self.isHidden = false
  374. self.playerTopToolBarView.isHidden = false
  375. })
  376. }
  377. func isShow() -> Bool {
  378. return !self.isHidden
  379. }
  380. public func hide() {
  381. UIView.animate(withDuration: 0.3, animations: {
  382. self.alpha = 0
  383. self.playerTopToolBarView.alpha = 0
  384. }, completion: { (value: Bool) in
  385. self.isHidden = true
  386. self.playerTopToolBarView.isHidden = true
  387. })
  388. }
  389. @objc private func automaticHide() {
  390. if let metadata = self.metadata {
  391. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterHidePlayerToolBar, userInfo: ["ocId":metadata.ocId])
  392. }
  393. }
  394. private func startTimerAutoHide() {
  395. timerAutoHide?.invalidate()
  396. timerAutoHide = Timer.scheduledTimer(timeInterval: 3.5, target: self, selector: #selector(automaticHide), userInfo: nil, repeats: false)
  397. }
  398. private func reStartTimerAutoHide() {
  399. if let timerAutoHide = timerAutoHide, timerAutoHide.isValid {
  400. startTimerAutoHide()
  401. }
  402. }
  403. func skip(seconds: Float64) {
  404. guard let ncplayer = ncplayer else { return }
  405. guard let player = appDelegate.player else { return }
  406. let currentTime = player.currentTime()
  407. var newTime: CMTime = .zero
  408. let timeToAdd: CMTime = CMTimeMakeWithSeconds(abs(seconds), preferredTimescale: 1)
  409. if seconds > 0 {
  410. newTime = CMTimeAdd(currentTime, timeToAdd)
  411. if newTime < durationTime {
  412. ncplayer.videoSeek(time: newTime)
  413. } else if newTime >= durationTime {
  414. let timeToSubtract: CMTime = CMTimeMakeWithSeconds(3, preferredTimescale: 1)
  415. newTime = CMTimeSubtract(durationTime, timeToSubtract)
  416. if newTime > currentTime {
  417. ncplayer.videoSeek(time: newTime)
  418. }
  419. }
  420. } else {
  421. newTime = CMTimeSubtract(currentTime, timeToAdd)
  422. ncplayer.videoSeek(time: newTime)
  423. }
  424. reStartTimerAutoHide()
  425. }
  426. //MARK: - Event / Gesture
  427. @objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
  428. if let touchEvent = event.allTouches?.first, let ncplayer = ncplayer {
  429. let seconds: Int64 = Int64(self.playbackSlider.value)
  430. let targetTime: CMTime = CMTimeMake(value: seconds, timescale: 1000)
  431. switch touchEvent.phase {
  432. case .began:
  433. wasInPlay = ncplayer.isPlay()
  434. ncplayer.playerPause()
  435. playbackSliderEvent = .began
  436. case .moved:
  437. ncplayer.videoSeek(time: targetTime)
  438. playbackSliderEvent = .moved
  439. case .ended:
  440. ncplayer.videoSeek(time: targetTime)
  441. if wasInPlay {
  442. ncplayer.playerPlay()
  443. }
  444. playbackSliderEvent = .ended
  445. default:
  446. break
  447. }
  448. reStartTimerAutoHide()
  449. }
  450. }
  451. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  452. hide()
  453. }
  454. //MARK: - Action
  455. @IBAction func buttonTouchInside(_ sender: UIButton) {
  456. }
  457. @IBAction func playerPause(_ sender: Any) {
  458. if appDelegate.player?.timeControlStatus == .playing {
  459. ncplayer?.playerPause()
  460. ncplayer?.saveCurrentTime()
  461. timerAutoHide?.invalidate()
  462. } else if appDelegate.player?.timeControlStatus == .paused {
  463. ncplayer?.playerPlay()
  464. startTimerAutoHide()
  465. } else if appDelegate.player?.timeControlStatus == .waitingToPlayAtSpecifiedRate {
  466. print("timeControlStatus.waitingToPlayAtSpecifiedRate")
  467. if let reason = appDelegate.player?.reasonForWaitingToPlay {
  468. switch reason {
  469. case .evaluatingBufferingRate:
  470. print("reasonForWaitingToPlay.evaluatingBufferingRate")
  471. case .toMinimizeStalls:
  472. print("reasonForWaitingToPlay.toMinimizeStalls")
  473. case .noItemToPlay:
  474. print("reasonForWaitingToPlay.noItemToPlay")
  475. default:
  476. print("Unknown \(reason)")
  477. }
  478. }
  479. }
  480. }
  481. @IBAction func setMute(_ sender: Any) {
  482. let mute = CCUtility.getAudioMute()
  483. CCUtility.setAudioMute(!mute)
  484. appDelegate.player?.isMuted = !mute
  485. updateToolBar()
  486. reStartTimerAutoHide()
  487. }
  488. @IBAction func setPip(_ sender: Any) {
  489. guard let metadata = self.metadata else { return }
  490. ncplayer?.pictureInPictureController?.startPictureInPicture()
  491. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterHidePlayerToolBar, userInfo: ["ocId":metadata.ocId])
  492. }
  493. @IBAction func forwardButtonSec(_ sender: Any) {
  494. skip(seconds: 10)
  495. }
  496. @IBAction func backButtonSec(_ sender: Any) {
  497. skip(seconds: -10)
  498. }
  499. }