NCPlayerToolBar.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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: - Event / Gesture
  314. @objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
  315. if let touchEvent = event.allTouches?.first, let ncplayer = ncplayer {
  316. let seconds: Int64 = Int64(self.playbackSlider.value)
  317. let targetTime: CMTime = CMTimeMake(value: seconds, timescale: 1)
  318. switch touchEvent.phase {
  319. case .began:
  320. wasInPlay = ncplayer.isPlay()
  321. ncplayer.playerPause()
  322. playbackSliderEvent = .began
  323. case .moved:
  324. ncplayer.videoSeek(time: targetTime)
  325. playbackSliderEvent = .moved
  326. case .ended:
  327. ncplayer.videoSeek(time: targetTime)
  328. if wasInPlay {
  329. ncplayer.playerPlay()
  330. }
  331. playbackSliderEvent = .ended
  332. default:
  333. break
  334. }
  335. reStartTimerAutoHide()
  336. }
  337. }
  338. // MARK: - Action
  339. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  340. // nothing
  341. }
  342. @IBAction func buttonPlayerToolBarTouchInside(_ sender: UIButton) {
  343. // nothing
  344. }
  345. @IBAction func buttonPlayerTopToolBarTouchInside(_ sender: UIButton) {
  346. // nothing
  347. }
  348. @IBAction func playerPause(_ sender: Any) {
  349. if ncplayer?.player?.timeControlStatus == .playing {
  350. ncplayer?.playerPause()
  351. ncplayer?.saveCurrentTime()
  352. timerAutoHide?.invalidate()
  353. } else if ncplayer?.player?.timeControlStatus == .paused {
  354. ncplayer?.playerPlay()
  355. startTimerAutoHide()
  356. } else if ncplayer?.player?.timeControlStatus == .waitingToPlayAtSpecifiedRate {
  357. print("timeControlStatus.waitingToPlayAtSpecifiedRate")
  358. if let reason = ncplayer?.player?.reasonForWaitingToPlay {
  359. switch reason {
  360. case .evaluatingBufferingRate:
  361. print("reasonForWaitingToPlay.evaluatingBufferingRate")
  362. case .toMinimizeStalls:
  363. print("reasonForWaitingToPlay.toMinimizeStalls")
  364. case .noItemToPlay:
  365. print("reasonForWaitingToPlay.noItemToPlay")
  366. default:
  367. print("Unknown \(reason)")
  368. }
  369. }
  370. }
  371. }
  372. @IBAction func setMute(_ sender: Any) {
  373. let mute = CCUtility.getAudioMute()
  374. CCUtility.setAudioMute(!mute)
  375. ncplayer?.player?.isMuted = !mute
  376. updateToolBar()
  377. reStartTimerAutoHide()
  378. }
  379. @IBAction func setPip(_ sender: Any) {
  380. guard let videoLayer = ncplayer?.videoLayer else { return }
  381. if let pictureInPictureController = self.pictureInPictureController, pictureInPictureController.isPictureInPictureActive {
  382. pictureInPictureController.stopPictureInPicture()
  383. }
  384. if pictureInPictureController == nil {
  385. pictureInPictureController = AVPictureInPictureController(playerLayer: videoLayer)
  386. pictureInPictureController?.delegate = self
  387. }
  388. if let pictureInPictureController = pictureInPictureController, pictureInPictureController.isPictureInPicturePossible, let metadata = self.metadata {
  389. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  390. pictureInPictureController.startPictureInPicture()
  391. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterHidePlayerToolBar, userInfo: ["ocId": metadata.ocId])
  392. }
  393. }
  394. }
  395. @IBAction func forwardButtonSec(_ sender: Any) {
  396. skip(seconds: 10)
  397. /*
  398. if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  399. skip(seconds: 10)
  400. } else if metadata?.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  401. forward()
  402. }
  403. */
  404. }
  405. @IBAction func backButtonSec(_ sender: Any) {
  406. skip(seconds: -10)
  407. /*
  408. if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  409. skip(seconds: -10)
  410. } else if metadata?.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  411. backward()
  412. }
  413. */
  414. }
  415. @IBAction func playerMessageButtonTouchInside(_ sender: UIButton) {
  416. #if MFFFLIB
  417. if let metadata = metadata {
  418. MFFF.shared.stopMFFFSession(url: URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)))
  419. }
  420. #endif
  421. }
  422. func forward() {
  423. var index: Int = 0
  424. if let currentIndex = self.viewerMediaPage?.currentIndex, let metadatas = self.viewerMediaPage?.metadatas, let ncplayer = self.ncplayer {
  425. if currentIndex == metadatas.count - 1 {
  426. index = 0
  427. } else {
  428. index = currentIndex + 1
  429. }
  430. self.viewerMediaPage?.goTo(index: index, direction: .forward, autoPlay: ncplayer.isPlay())
  431. }
  432. }
  433. func backward() {
  434. var index: Int = 0
  435. if let currentIndex = self.viewerMediaPage?.currentIndex, let metadatas = self.viewerMediaPage?.metadatas, let ncplayer = self.ncplayer {
  436. if currentIndex == 0 {
  437. index = metadatas.count - 1
  438. } else {
  439. index = currentIndex - 1
  440. }
  441. self.viewerMediaPage?.goTo(index: index, direction: .reverse, autoPlay: ncplayer.isPlay())
  442. }
  443. }
  444. }
  445. extension NCPlayerToolBar: AVPictureInPictureControllerDelegate {
  446. func pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
  447. if let metadata = self.metadata, let ncplayer = self.ncplayer, !ncplayer.isPlay() {
  448. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId": metadata.ocId, "enableTimerAutoHide": false])
  449. }
  450. }
  451. }