NCPlayerToolBar.swift 24 KB

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