NCPlayerToolBar.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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, timeSeek: CMTime,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. appDelegate.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. appDelegate.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. appDelegate.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. appDelegate.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 = appDelegate.playCommand {
  261. MPRemoteCommandCenter.shared().playCommand.removeTarget(playCommand)
  262. appDelegate.playCommand = nil
  263. }
  264. if let pauseCommand = appDelegate.pauseCommand {
  265. MPRemoteCommandCenter.shared().pauseCommand.removeTarget(pauseCommand)
  266. appDelegate.pauseCommand = nil
  267. }
  268. if let skipForwardCommand = appDelegate.skipForwardCommand {
  269. MPRemoteCommandCenter.shared().skipForwardCommand.removeTarget(skipForwardCommand)
  270. appDelegate.skipForwardCommand = nil
  271. }
  272. if let skipBackwardCommand = appDelegate.skipBackwardCommand {
  273. MPRemoteCommandCenter.shared().skipBackwardCommand.removeTarget(skipBackwardCommand)
  274. appDelegate.skipBackwardCommand = nil
  275. }
  276. if let nextTrackCommand = appDelegate.nextTrackCommand {
  277. MPRemoteCommandCenter.shared().nextTrackCommand.removeTarget(nextTrackCommand)
  278. appDelegate.nextTrackCommand = nil
  279. }
  280. if let previousTrackCommand = appDelegate.previousTrackCommand {
  281. MPRemoteCommandCenter.shared().previousTrackCommand.removeTarget(previousTrackCommand)
  282. appDelegate.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. }
  348. func isShow() -> Bool {
  349. return !self.isHidden
  350. }
  351. public func hide() {
  352. UIView.animate(withDuration: 0.3, animations: {
  353. self.alpha = 0
  354. self.playerTopToolBarView.alpha = 0
  355. }, completion: { (value: Bool) in
  356. self.isHidden = true
  357. self.playerTopToolBarView.isHidden = true
  358. })
  359. }
  360. @objc private func automaticHide() {
  361. if let metadata = self.metadata {
  362. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterHidePlayerToolBar, userInfo: ["ocId":metadata.ocId])
  363. }
  364. }
  365. private func startTimerAutoHide() {
  366. timerAutoHide?.invalidate()
  367. timerAutoHide = Timer.scheduledTimer(timeInterval: 3.5, target: self, selector: #selector(automaticHide), userInfo: nil, repeats: false)
  368. }
  369. private func reStartTimerAutoHide() {
  370. if let timerAutoHide = timerAutoHide, timerAutoHide.isValid {
  371. startTimerAutoHide()
  372. }
  373. }
  374. func skip(seconds: Float64) {
  375. guard let ncplayer = ncplayer else { return }
  376. guard let player = ncplayer.player else { return }
  377. let currentTime = player.currentTime()
  378. var newTime: CMTime = .zero
  379. let timeToAdd: CMTime = CMTimeMakeWithSeconds(abs(seconds), preferredTimescale: 1)
  380. if seconds > 0 {
  381. newTime = CMTimeAdd(currentTime, timeToAdd)
  382. if newTime < ncplayer.durationTime {
  383. ncplayer.videoSeek(time: newTime)
  384. } else if newTime >= ncplayer.durationTime {
  385. let timeToSubtract: CMTime = CMTimeMakeWithSeconds(3, preferredTimescale: 1)
  386. newTime = CMTimeSubtract(ncplayer.durationTime, timeToSubtract)
  387. if newTime > currentTime {
  388. ncplayer.videoSeek(time: newTime)
  389. }
  390. }
  391. } else {
  392. newTime = CMTimeSubtract(currentTime, timeToAdd)
  393. ncplayer.videoSeek(time: newTime)
  394. }
  395. reStartTimerAutoHide()
  396. }
  397. func forward() {
  398. var index: Int = 0
  399. if let currentIndex = self.viewerMedia?.currentIndex, let metadatas = self.viewerMedia?.metadatas, let ncplayer = self.ncplayer {
  400. if currentIndex == metadatas.count - 1 {
  401. index = 0
  402. } else {
  403. index = currentIndex + 1
  404. }
  405. self.viewerMedia?.goTo(index: index, direction: .forward, autoPlay: ncplayer.isPlay())
  406. }
  407. }
  408. func backward() {
  409. var index: Int = 0
  410. if let currentIndex = self.viewerMedia?.currentIndex, let metadatas = self.viewerMedia?.metadatas, let ncplayer = self.ncplayer {
  411. if currentIndex == 0 {
  412. index = metadatas.count - 1
  413. } else {
  414. index = currentIndex - 1
  415. }
  416. self.viewerMedia?.goTo(index: index, direction: .reverse, autoPlay: ncplayer.isPlay())
  417. }
  418. }
  419. //MARK: - Event / Gesture
  420. @objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
  421. if let touchEvent = event.allTouches?.first, let ncplayer = ncplayer {
  422. let seconds: Int64 = Int64(self.playbackSlider.value)
  423. let targetTime: CMTime = CMTimeMake(value: seconds, timescale: 1)
  424. switch touchEvent.phase {
  425. case .began:
  426. wasInPlay = ncplayer.isPlay()
  427. ncplayer.playerPause()
  428. playbackSliderEvent = .began
  429. case .moved:
  430. ncplayer.videoSeek(time: targetTime)
  431. playbackSliderEvent = .moved
  432. case .ended:
  433. ncplayer.videoSeek(time: targetTime)
  434. if wasInPlay {
  435. ncplayer.playerPlay()
  436. }
  437. playbackSliderEvent = .ended
  438. default:
  439. break
  440. }
  441. reStartTimerAutoHide()
  442. }
  443. }
  444. //MARK: - Action
  445. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  446. }
  447. @IBAction func buttonPlayerToolBarTouchInside(_ sender: UIButton) {
  448. }
  449. @IBAction func buttonPlayerTopToolBarTouchInside(_ sender: UIButton) {
  450. }
  451. @IBAction func playerPause(_ sender: Any) {
  452. if ncplayer?.player?.timeControlStatus == .playing {
  453. ncplayer?.playerPause()
  454. ncplayer?.saveCurrentTime()
  455. timerAutoHide?.invalidate()
  456. } else if ncplayer?.player?.timeControlStatus == .paused {
  457. ncplayer?.playerPlay()
  458. startTimerAutoHide()
  459. } else if ncplayer?.player?.timeControlStatus == .waitingToPlayAtSpecifiedRate {
  460. print("timeControlStatus.waitingToPlayAtSpecifiedRate")
  461. if let reason = ncplayer?.player?.reasonForWaitingToPlay {
  462. switch reason {
  463. case .evaluatingBufferingRate:
  464. print("reasonForWaitingToPlay.evaluatingBufferingRate")
  465. case .toMinimizeStalls:
  466. print("reasonForWaitingToPlay.toMinimizeStalls")
  467. case .noItemToPlay:
  468. print("reasonForWaitingToPlay.noItemToPlay")
  469. default:
  470. print("Unknown \(reason)")
  471. }
  472. }
  473. }
  474. }
  475. @IBAction func setMute(_ sender: Any) {
  476. let mute = CCUtility.getAudioMute()
  477. CCUtility.setAudioMute(!mute)
  478. ncplayer?.player?.isMuted = !mute
  479. updateToolBar()
  480. reStartTimerAutoHide()
  481. }
  482. @IBAction func setPip(_ sender: Any) {
  483. guard let metadata = self.metadata else { return }
  484. ncplayer?.pictureInPictureController?.startPictureInPicture()
  485. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterHidePlayerToolBar, userInfo: ["ocId":metadata.ocId])
  486. }
  487. @IBAction func forwardButtonSec(_ sender: Any) {
  488. skip(seconds: 10)
  489. /*
  490. if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  491. skip(seconds: 10)
  492. } else if metadata?.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  493. forward()
  494. }
  495. */
  496. }
  497. @IBAction func backButtonSec(_ sender: Any) {
  498. skip(seconds: -10)
  499. /*
  500. if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  501. skip(seconds: -10)
  502. } else if metadata?.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  503. backward()
  504. }
  505. */
  506. }
  507. }