NCPlayerToolBar.swift 24 KB

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