NCPlayerToolBar.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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 playerMessageTitleTopConstraint: NSLayoutConstraint!
  44. @IBOutlet weak var playerMessageDescription: UILabel!
  45. @IBOutlet weak var playerMessageButton: UIButton!
  46. @IBOutlet weak var playerMessageCommentBottomConstraint: NSLayoutConstraint!
  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 else { return }
  230. if ncplayer == nil { return }
  231. if metadata.livePhoto { return }
  232. if metadata.classFile != NCCommunicationCommon.typeClassFile.video.rawValue && metadata.classFile != NCCommunicationCommon.typeClassFile.audio.rawValue { return }
  233. #if MFFFLIB
  234. if MFFF.shared.existsMFFFSession(url: URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))) {
  235. self.hide()
  236. return
  237. }
  238. #endif
  239. timerAutoHide?.invalidate()
  240. if enableTimerAutoHide {
  241. startTimerAutoHide()
  242. }
  243. if !self.isHidden { return }
  244. UIView.animate(withDuration: 0.3, animations: {
  245. self.alpha = 1
  246. self.playerTopToolBarView.alpha = 1
  247. }, completion: { (_: Bool) in
  248. self.isHidden = false
  249. self.playerTopToolBarView.isHidden = false
  250. })
  251. updateToolBar()
  252. }
  253. func isShow() -> Bool {
  254. return !self.isHidden
  255. }
  256. public func hide() {
  257. UIView.animate(withDuration: 0.3, animations: {
  258. self.alpha = 0
  259. self.playerTopToolBarView.alpha = 0
  260. }, completion: { (_: Bool) in
  261. self.isHidden = true
  262. self.playerTopToolBarView.isHidden = true
  263. })
  264. }
  265. @objc private func automaticHide() {
  266. if let metadata = self.metadata {
  267. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterHidePlayerToolBar, userInfo: ["ocId": metadata.ocId])
  268. }
  269. }
  270. private func startTimerAutoHide() {
  271. timerAutoHide?.invalidate()
  272. timerAutoHide = Timer.scheduledTimer(timeInterval: 3.5, target: self, selector: #selector(automaticHide), userInfo: nil, repeats: false)
  273. }
  274. private func reStartTimerAutoHide() {
  275. if let timerAutoHide = timerAutoHide, timerAutoHide.isValid {
  276. startTimerAutoHide()
  277. }
  278. }
  279. func skip(seconds: Float64) {
  280. guard let ncplayer = ncplayer else { return }
  281. guard let player = ncplayer.player else { return }
  282. let currentTime = player.currentTime()
  283. var newTime: CMTime = .zero
  284. let timeToAdd: CMTime = CMTimeMakeWithSeconds(abs(seconds), preferredTimescale: 1)
  285. if seconds > 0 {
  286. newTime = CMTimeAdd(currentTime, timeToAdd)
  287. if newTime < ncplayer.durationTime {
  288. ncplayer.videoSeek(time: newTime)
  289. } else if newTime >= ncplayer.durationTime {
  290. let timeToSubtract: CMTime = CMTimeMakeWithSeconds(3, preferredTimescale: 1)
  291. newTime = CMTimeSubtract(ncplayer.durationTime, timeToSubtract)
  292. if newTime > currentTime {
  293. ncplayer.videoSeek(time: newTime)
  294. }
  295. }
  296. } else {
  297. newTime = CMTimeSubtract(currentTime, timeToAdd)
  298. if newTime.seconds < 0 {
  299. newTime = .zero
  300. }
  301. ncplayer.videoSeek(time: newTime)
  302. }
  303. updateToolBar()
  304. reStartTimerAutoHide()
  305. }
  306. func isPictureInPictureActive() -> Bool {
  307. if let pictureInPictureController = self.pictureInPictureController, pictureInPictureController.isPictureInPictureActive {
  308. return true
  309. } else {
  310. return false
  311. }
  312. }
  313. func stopTimerAutoHide() {
  314. timerAutoHide?.invalidate()
  315. }
  316. // MARK: - Message
  317. func showMessage(_ title: String, description: String?, backgroundColor: UIColor = NCBrandColor.shared.brand, isHiddenPregress: Bool = true, hiddenAfterSeconds: Double = 0) {
  318. self.playerMessageHeightConstraint.constant = 120
  319. self.playerMessage.backgroundColor = backgroundColor
  320. self.playerMessageTitle.text = NSLocalizedString(title, comment: "")
  321. self.playerMessageTitle.textColor = NCBrandColor.shared.brandText
  322. self.playerMessageButton.isHidden = isHiddenPregress
  323. self.playerMessageButton.setBackgroundImage(UIImage(named: "stop")!.image(color: .black, size: 30), for: .normal)
  324. if let description = description {
  325. self.playerMessageDescription.isHidden = false
  326. self.playerMessageDescription.text = NSLocalizedString(description, comment: "")
  327. self.playerMessageTitleTopConstraint.constant = 8
  328. } else {
  329. self.playerMessageDescription.isHidden = true
  330. self.playerMessageHeightConstraint.constant = 65
  331. self.playerMessageDescription.text = ""
  332. self.playerMessageTitleTopConstraint.constant = 20
  333. }
  334. self.playerMessageDescription.textColor = NCBrandColor.shared.brandText
  335. self.playerMessageProgressView.progress = 0
  336. self.playerMessageProgressView.tintColor = .black
  337. self.playerMessageProgressView.isHidden = isHiddenPregress
  338. self.playerMessageCommentBottomConstraint.constant = 30
  339. if isHiddenPregress {
  340. self.playerMessageCommentBottomConstraint.constant = 5
  341. self.playerMessageHeightConstraint.constant = 90
  342. }
  343. UIView.animate(withDuration: 0.3, animations: {
  344. self.playerMessage.alpha = 1
  345. }, completion: { (_: Bool) in
  346. self.playerMessage.isHidden = false
  347. })
  348. timerAutoHideMessage?.invalidate()
  349. if hiddenAfterSeconds > 0 {
  350. timerAutoHideMessage = Timer.scheduledTimer(timeInterval: hiddenAfterSeconds, target: self, selector: #selector(hideMessage), userInfo: nil, repeats: false)
  351. }
  352. }
  353. @objc func hideMessage() {
  354. self.playerMessageProgressView.progress = 0
  355. UIView.animate(withDuration: 0.3, animations: {
  356. self.playerMessage.alpha = 0
  357. }, completion: { (_: Bool) in
  358. self.playerMessage.isHidden = true
  359. })
  360. }
  361. func showMessageProgress(_ progress: Float) {
  362. self.playerMessageProgressView.progress = progress
  363. }
  364. func isHiddenMessage() -> Bool {
  365. timerAutoHideMessage?.invalidate()
  366. return self.playerMessage.isHidden
  367. }
  368. // MARK: - Event / Gesture
  369. @objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
  370. if let touchEvent = event.allTouches?.first, let ncplayer = ncplayer {
  371. let seconds: Int64 = Int64(self.playbackSlider.value)
  372. let targetTime: CMTime = CMTimeMake(value: seconds, timescale: 1)
  373. switch touchEvent.phase {
  374. case .began:
  375. wasInPlay = ncplayer.isPlay()
  376. ncplayer.playerPause()
  377. playbackSliderEvent = .began
  378. case .moved:
  379. ncplayer.videoSeek(time: targetTime)
  380. playbackSliderEvent = .moved
  381. case .ended:
  382. ncplayer.videoSeek(time: targetTime)
  383. if wasInPlay {
  384. ncplayer.playerPlay()
  385. }
  386. playbackSliderEvent = .ended
  387. default:
  388. break
  389. }
  390. reStartTimerAutoHide()
  391. }
  392. }
  393. // MARK: - Action
  394. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  395. // nothing
  396. }
  397. @IBAction func buttonPlayerToolBarTouchInside(_ sender: UIButton) {
  398. // nothing
  399. }
  400. @IBAction func buttonPlayerTopToolBarTouchInside(_ sender: UIButton) {
  401. // nothing
  402. }
  403. @IBAction func playerPause(_ sender: Any) {
  404. if ncplayer?.player?.timeControlStatus == .playing {
  405. ncplayer?.playerPause()
  406. ncplayer?.saveCurrentTime()
  407. timerAutoHide?.invalidate()
  408. } else if ncplayer?.player?.timeControlStatus == .paused {
  409. ncplayer?.playerPlay()
  410. startTimerAutoHide()
  411. } else if ncplayer?.player?.timeControlStatus == .waitingToPlayAtSpecifiedRate {
  412. print("timeControlStatus.waitingToPlayAtSpecifiedRate")
  413. if let reason = ncplayer?.player?.reasonForWaitingToPlay {
  414. switch reason {
  415. case .evaluatingBufferingRate:
  416. print("reasonForWaitingToPlay.evaluatingBufferingRate")
  417. case .toMinimizeStalls:
  418. print("reasonForWaitingToPlay.toMinimizeStalls")
  419. case .noItemToPlay:
  420. print("reasonForWaitingToPlay.noItemToPlay")
  421. default:
  422. print("Unknown \(reason)")
  423. }
  424. }
  425. }
  426. }
  427. @IBAction func setMute(_ sender: Any) {
  428. let mute = CCUtility.getAudioMute()
  429. CCUtility.setAudioMute(!mute)
  430. ncplayer?.player?.isMuted = !mute
  431. updateToolBar()
  432. reStartTimerAutoHide()
  433. }
  434. @IBAction func setPip(_ sender: Any) {
  435. guard let videoLayer = ncplayer?.videoLayer else { return }
  436. if let pictureInPictureController = self.pictureInPictureController, pictureInPictureController.isPictureInPictureActive {
  437. pictureInPictureController.stopPictureInPicture()
  438. }
  439. if pictureInPictureController == nil {
  440. pictureInPictureController = AVPictureInPictureController(playerLayer: videoLayer)
  441. pictureInPictureController?.delegate = self
  442. }
  443. if let pictureInPictureController = pictureInPictureController, pictureInPictureController.isPictureInPicturePossible, let metadata = self.metadata {
  444. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  445. pictureInPictureController.startPictureInPicture()
  446. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterHidePlayerToolBar, userInfo: ["ocId": metadata.ocId])
  447. }
  448. }
  449. }
  450. @IBAction func forwardButtonSec(_ sender: Any) {
  451. skip(seconds: 10)
  452. /*
  453. if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  454. skip(seconds: 10)
  455. } else if metadata?.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  456. forward()
  457. }
  458. */
  459. }
  460. @IBAction func backButtonSec(_ sender: Any) {
  461. skip(seconds: -10)
  462. /*
  463. if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  464. skip(seconds: -10)
  465. } else if metadata?.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  466. backward()
  467. }
  468. */
  469. }
  470. @IBAction func playerMessageButtonTouchInside(_ sender: UIButton) {
  471. #if MFFFLIB
  472. if let metadata = metadata {
  473. MFFF.shared.stopMFFFSession(url: URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)))
  474. }
  475. #endif
  476. }
  477. func forward() {
  478. var index: Int = 0
  479. if let currentIndex = self.viewerMediaPage?.currentIndex, let metadatas = self.viewerMediaPage?.metadatas, let ncplayer = self.ncplayer {
  480. if currentIndex == metadatas.count - 1 {
  481. index = 0
  482. } else {
  483. index = currentIndex + 1
  484. }
  485. self.viewerMediaPage?.goTo(index: index, direction: .forward, autoPlay: ncplayer.isPlay())
  486. }
  487. }
  488. func backward() {
  489. var index: Int = 0
  490. if let currentIndex = self.viewerMediaPage?.currentIndex, let metadatas = self.viewerMediaPage?.metadatas, let ncplayer = self.ncplayer {
  491. if currentIndex == 0 {
  492. index = metadatas.count - 1
  493. } else {
  494. index = currentIndex - 1
  495. }
  496. self.viewerMediaPage?.goTo(index: index, direction: .reverse, autoPlay: ncplayer.isPlay())
  497. }
  498. }
  499. }
  500. extension NCPlayerToolBar: AVPictureInPictureControllerDelegate {
  501. func pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
  502. if let metadata = self.metadata, let ncplayer = self.ncplayer, !ncplayer.isPlay() {
  503. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId": metadata.ocId, "enableTimerAutoHide": false])
  504. }
  505. }
  506. }