NCPlayerToolBar.swift 20 KB

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