NCPlayerToolBar.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. //
  2. // NCPlayerToolBar.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 01/07/21.
  6. // Copyright © 2021 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import NCCommunication
  25. import CoreMedia
  26. import UIKit
  27. import AVKit
  28. import MediaPlayer
  29. class NCPlayerToolBar: UIView {
  30. @IBOutlet weak var playerTopToolBarView: UIView!
  31. @IBOutlet weak var pipButton: UIButton!
  32. @IBOutlet weak var muteButton: UIButton!
  33. @IBOutlet weak var playButton: UIButton!
  34. @IBOutlet weak var forwardButton: UIButton!
  35. @IBOutlet weak var backButton: UIButton!
  36. @IBOutlet weak var playbackSlider: UISlider!
  37. @IBOutlet weak var labelLeftTime: UILabel!
  38. @IBOutlet weak var labelCurrentTime: UILabel!
  39. enum sliderEventType {
  40. case began
  41. case ended
  42. case moved
  43. }
  44. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  45. private var ncplayer: NCPlayer?
  46. private var wasInPlay: Bool = false
  47. private var playbackSliderEvent: sliderEventType = .ended
  48. private var timerAutoHide: Timer?
  49. private var metadata: tableMetadata?
  50. private var image: UIImage?
  51. var pictureInPictureController: AVPictureInPictureController?
  52. weak var viewerMedia: NCViewerMedia?
  53. // MARK: - View Life Cycle
  54. override func awakeFromNib() {
  55. super.awakeFromNib()
  56. // for disable gesture of UIPageViewController
  57. let panRecognizer = UIPanGestureRecognizer(target: self, action: nil)
  58. addGestureRecognizer(panRecognizer)
  59. let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSingleTapWith(gestureRecognizer:)))
  60. addGestureRecognizer(singleTapGestureRecognizer)
  61. self.layer.cornerRadius = 15
  62. self.layer.masksToBounds = true
  63. let blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
  64. blurEffectView.frame = self.bounds
  65. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  66. self.insertSubview(blurEffectView, at:0)
  67. playerTopToolBarView.layer.cornerRadius = 10
  68. playerTopToolBarView.layer.masksToBounds = true
  69. let blurEffectTopToolBarView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
  70. blurEffectTopToolBarView.frame = playerTopToolBarView.bounds
  71. blurEffectTopToolBarView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  72. playerTopToolBarView.insertSubview(blurEffectTopToolBarView, at:0)
  73. pipButton.setImage(NCUtility.shared.loadImage(named: "pip.enter", color: .lightGray), for: .normal)
  74. pipButton.isEnabled = false
  75. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .lightGray), for: .normal)
  76. muteButton.isEnabled = false
  77. playbackSlider.value = 0
  78. playbackSlider.minimumValue = 0
  79. playbackSlider.maximumValue = 0
  80. playbackSlider.isContinuous = true
  81. playbackSlider.tintColor = .lightGray
  82. playbackSlider.isEnabled = false
  83. labelCurrentTime.text = NCUtility.shared.stringFromTime(.zero)
  84. labelCurrentTime.textColor = .lightGray
  85. labelLeftTime.text = NCUtility.shared.stringFromTime(.zero)
  86. labelLeftTime.textColor = .lightGray
  87. backButton.isEnabled = false
  88. playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .lightGray), for: .normal)
  89. playButton.isEnabled = false
  90. forwardButton.isEnabled = false
  91. NotificationCenter.default.addObserver(self, selector: #selector(handleInterruption), name: AVAudioSession.interruptionNotification, object: nil)
  92. NotificationCenter.default.addObserver(self, selector: #selector(handleRouteChange), name: AVAudioSession.routeChangeNotification, object: nil)
  93. }
  94. deinit {
  95. print("deinit NCPlayerToolBar")
  96. NotificationCenter.default.removeObserver(self, name: AVAudioSession.interruptionNotification, object: nil)
  97. NotificationCenter.default.removeObserver(self, name: AVAudioSession.routeChangeNotification, object: nil)
  98. }
  99. // MARK: -
  100. func setBarPlayer(ncplayer: NCPlayer, metadata: tableMetadata, image: UIImage?) {
  101. self.ncplayer = ncplayer
  102. self.metadata = metadata
  103. self.image = image
  104. playbackSlider.value = 0
  105. playbackSlider.minimumValue = 0
  106. playbackSlider.maximumValue = Float(ncplayer.durationTime.seconds)
  107. playbackSlider.addTarget(self, action: #selector(onSliderValChanged(slider:event:)), for: .valueChanged)
  108. labelCurrentTime.text = NCUtility.shared.stringFromTime(.zero)
  109. labelLeftTime.text = "-" + NCUtility.shared.stringFromTime(ncplayer.durationTime)
  110. updateToolBar()
  111. }
  112. public func updateToolBar() {
  113. guard let metadata = self.metadata else { return }
  114. guard let ncplayer = self.ncplayer else { return }
  115. // MUTE
  116. if CCUtility.getAudioMute() {
  117. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .white), for: .normal)
  118. } else {
  119. muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
  120. }
  121. muteButton.isEnabled = true
  122. // PIP
  123. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue && AVPictureInPictureController.isPictureInPictureSupported() {
  124. pipButton.setImage(NCUtility.shared.loadImage(named: "pip.enter", color: .white), for: .normal)
  125. pipButton.isEnabled = true
  126. } else {
  127. pipButton.setImage(NCUtility.shared.loadImage(named: "pip.enter", color: .gray), for: .normal)
  128. pipButton.isEnabled = false
  129. }
  130. // SLIDER TIME (START - END)
  131. let time = (ncplayer.player?.currentTime() ?? .zero).convertScale(1000, method: .default)
  132. playbackSlider.value = Float(time.seconds)
  133. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = time.seconds
  134. playbackSlider.isEnabled = true
  135. labelCurrentTime.text = NCUtility.shared.stringFromTime(time)
  136. labelLeftTime.text = "-" + NCUtility.shared.stringFromTime(ncplayer.durationTime - time)
  137. // BACK
  138. if #available(iOS 13.0, *) {
  139. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.10", color: .white), for: .normal)
  140. } else {
  141. backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.10", color: .white, size: 30), for: .normal)
  142. }
  143. backButton.isEnabled = true
  144. // PLAY
  145. if ncplayer.isPlay() {
  146. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 1
  147. } else {
  148. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0
  149. }
  150. let namedPlay = ncplayer.isPlay() ? "pause.fill" : "play.fill"
  151. if #available(iOS 13.0, *) {
  152. playButton.setImage(NCUtility.shared.loadImage(named: namedPlay, color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
  153. } else {
  154. playButton.setImage(NCUtility.shared.loadImage(named: namedPlay, color: .white, size: 30), for: .normal)
  155. }
  156. playButton.isEnabled = true
  157. // FORWARD
  158. if #available(iOS 13.0, *) {
  159. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.10", color: .white), for: .normal)
  160. } else {
  161. forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.10", color: .white, size: 30), for: .normal)
  162. }
  163. forwardButton.isEnabled = true
  164. }
  165. // MARK: Handle Notifications
  166. @objc func handleRouteChange(notification: Notification) {
  167. guard let userInfo = notification.userInfo, let reasonValue = userInfo[AVAudioSessionRouteChangeReasonKey] as? UInt, let reason = AVAudioSession.RouteChangeReason(rawValue:reasonValue) else { return }
  168. switch reason {
  169. case .newDeviceAvailable:
  170. let session = AVAudioSession.sharedInstance()
  171. for output in session.currentRoute.outputs where output.portType == AVAudioSession.Port.headphones {
  172. print("headphones connected")
  173. DispatchQueue.main.sync {
  174. ncplayer?.playerPlay()
  175. startTimerAutoHide()
  176. }
  177. break
  178. }
  179. case .oldDeviceUnavailable:
  180. if let previousRoute = userInfo[AVAudioSessionRouteChangePreviousRouteKey] as? AVAudioSessionRouteDescription {
  181. for output in previousRoute.outputs where output.portType == AVAudioSession.Port.headphones {
  182. print("headphones disconnected")
  183. DispatchQueue.main.sync {
  184. ncplayer?.playerPause()
  185. ncplayer?.saveCurrentTime()
  186. }
  187. break
  188. }
  189. }
  190. default: ()
  191. }
  192. }
  193. @objc func handleInterruption(notification: Notification) {
  194. guard let userInfo = notification.userInfo, let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt, let type = AVAudioSession.InterruptionType(rawValue: typeValue) else { return }
  195. if type == .began {
  196. print("Interruption began")
  197. } else if type == .ended {
  198. if let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt {
  199. let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue)
  200. if options.contains(.shouldResume) {
  201. print("Interruption Ended - playback should resume")
  202. ncplayer?.playerPlay()
  203. startTimerAutoHide()
  204. } else {
  205. print("Interruption Ended - playback should NOT resume")
  206. }
  207. }
  208. }
  209. }
  210. // MARK: -
  211. public func show(enableTimerAutoHide: Bool = false) {
  212. guard let metadata = self.metadata else { return }
  213. if metadata.classFile != NCCommunicationCommon.typeClassFile.video.rawValue && metadata.classFile != NCCommunicationCommon.typeClassFile.audio.rawValue { return }
  214. if metadata.livePhoto { return }
  215. timerAutoHide?.invalidate()
  216. if enableTimerAutoHide {
  217. startTimerAutoHide()
  218. }
  219. if !self.isHidden { return }
  220. UIView.animate(withDuration: 0.3, animations: {
  221. self.alpha = 1
  222. self.playerTopToolBarView.alpha = 1
  223. }, completion: { (value: Bool) in
  224. self.isHidden = false
  225. self.playerTopToolBarView.isHidden = false
  226. })
  227. updateToolBar()
  228. }
  229. func isShow() -> Bool {
  230. return !self.isHidden
  231. }
  232. public func hide() {
  233. UIView.animate(withDuration: 0.3, animations: {
  234. self.alpha = 0
  235. self.playerTopToolBarView.alpha = 0
  236. }, completion: { (value: Bool) in
  237. self.isHidden = true
  238. self.playerTopToolBarView.isHidden = true
  239. })
  240. }
  241. @objc private func automaticHide() {
  242. if let metadata = self.metadata {
  243. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterHidePlayerToolBar, userInfo: ["ocId":metadata.ocId])
  244. }
  245. }
  246. private func startTimerAutoHide() {
  247. timerAutoHide?.invalidate()
  248. timerAutoHide = Timer.scheduledTimer(timeInterval: 3.5, target: self, selector: #selector(automaticHide), userInfo: nil, repeats: false)
  249. }
  250. private func reStartTimerAutoHide() {
  251. if let timerAutoHide = timerAutoHide, timerAutoHide.isValid {
  252. startTimerAutoHide()
  253. }
  254. }
  255. func skip(seconds: Float64) {
  256. guard let ncplayer = ncplayer else { return }
  257. guard let player = ncplayer.player else { return }
  258. let currentTime = player.currentTime()
  259. var newTime: CMTime = .zero
  260. let timeToAdd: CMTime = CMTimeMakeWithSeconds(abs(seconds), preferredTimescale: 1)
  261. if seconds > 0 {
  262. newTime = CMTimeAdd(currentTime, timeToAdd)
  263. if newTime < ncplayer.durationTime {
  264. ncplayer.videoSeek(time: newTime)
  265. } else if newTime >= ncplayer.durationTime {
  266. let timeToSubtract: CMTime = CMTimeMakeWithSeconds(3, preferredTimescale: 1)
  267. newTime = CMTimeSubtract(ncplayer.durationTime, timeToSubtract)
  268. if newTime > currentTime {
  269. ncplayer.videoSeek(time: newTime)
  270. }
  271. }
  272. } else {
  273. newTime = CMTimeSubtract(currentTime, timeToAdd)
  274. if newTime.seconds < 0 {
  275. newTime = .zero
  276. }
  277. ncplayer.videoSeek(time: newTime)
  278. }
  279. updateToolBar()
  280. reStartTimerAutoHide()
  281. }
  282. func isPictureInPictureActive() -> Bool {
  283. guard let pictureInPictureController = self.pictureInPictureController else { return false }
  284. if pictureInPictureController.isPictureInPictureActive {
  285. return true
  286. } else {
  287. return false
  288. }
  289. }
  290. func stopTimerAutoHide() {
  291. timerAutoHide?.invalidate()
  292. }
  293. //MARK: - Event / Gesture
  294. @objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
  295. if let touchEvent = event.allTouches?.first, let ncplayer = ncplayer {
  296. let seconds: Int64 = Int64(self.playbackSlider.value)
  297. let targetTime: CMTime = CMTimeMake(value: seconds, timescale: 1)
  298. switch touchEvent.phase {
  299. case .began:
  300. wasInPlay = ncplayer.isPlay()
  301. ncplayer.playerPause()
  302. playbackSliderEvent = .began
  303. case .moved:
  304. ncplayer.videoSeek(time: targetTime)
  305. playbackSliderEvent = .moved
  306. case .ended:
  307. ncplayer.videoSeek(time: targetTime)
  308. if wasInPlay {
  309. ncplayer.playerPlay()
  310. }
  311. playbackSliderEvent = .ended
  312. default:
  313. break
  314. }
  315. reStartTimerAutoHide()
  316. }
  317. }
  318. //MARK: - Action
  319. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  320. // nothing
  321. }
  322. @IBAction func buttonPlayerToolBarTouchInside(_ sender: UIButton) {
  323. // nothing
  324. }
  325. @IBAction func buttonPlayerTopToolBarTouchInside(_ sender: UIButton) {
  326. // nothing
  327. }
  328. @IBAction func playerPause(_ sender: Any) {
  329. if ncplayer?.player?.timeControlStatus == .playing {
  330. ncplayer?.playerPause()
  331. ncplayer?.saveCurrentTime()
  332. timerAutoHide?.invalidate()
  333. } else if ncplayer?.player?.timeControlStatus == .paused {
  334. ncplayer?.playerPlay()
  335. startTimerAutoHide()
  336. } else if ncplayer?.player?.timeControlStatus == .waitingToPlayAtSpecifiedRate {
  337. print("timeControlStatus.waitingToPlayAtSpecifiedRate")
  338. if let reason = ncplayer?.player?.reasonForWaitingToPlay {
  339. switch reason {
  340. case .evaluatingBufferingRate:
  341. print("reasonForWaitingToPlay.evaluatingBufferingRate")
  342. case .toMinimizeStalls:
  343. print("reasonForWaitingToPlay.toMinimizeStalls")
  344. case .noItemToPlay:
  345. print("reasonForWaitingToPlay.noItemToPlay")
  346. default:
  347. print("Unknown \(reason)")
  348. }
  349. }
  350. }
  351. }
  352. @IBAction func setMute(_ sender: Any) {
  353. let mute = CCUtility.getAudioMute()
  354. CCUtility.setAudioMute(!mute)
  355. ncplayer?.player?.isMuted = !mute
  356. updateToolBar()
  357. reStartTimerAutoHide()
  358. }
  359. @IBAction func setPip(_ sender: Any) {
  360. guard let metadata = self.metadata else { return }
  361. guard let ncplayer = self.ncplayer else { return }
  362. guard let videoLayer = ncplayer.videoLayer else { return }
  363. if let pictureInPictureController = self.pictureInPictureController, pictureInPictureController.isPictureInPictureActive {
  364. pictureInPictureController.stopPictureInPicture()
  365. }
  366. if pictureInPictureController == nil {
  367. pictureInPictureController = AVPictureInPictureController(playerLayer: videoLayer)
  368. pictureInPictureController?.delegate = self
  369. }
  370. if let pictureInPictureController = pictureInPictureController, pictureInPictureController.isPictureInPicturePossible {
  371. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
  372. pictureInPictureController.startPictureInPicture()
  373. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterHidePlayerToolBar, userInfo: ["ocId":metadata.ocId])
  374. }
  375. }
  376. }
  377. @IBAction func forwardButtonSec(_ sender: Any) {
  378. skip(seconds: 10)
  379. /*
  380. if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  381. skip(seconds: 10)
  382. } else if metadata?.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  383. forward()
  384. }
  385. */
  386. }
  387. @IBAction func backButtonSec(_ sender: Any) {
  388. skip(seconds: -10)
  389. /*
  390. if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  391. skip(seconds: -10)
  392. } else if metadata?.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  393. backward()
  394. }
  395. */
  396. }
  397. func forward() {
  398. var index: Int = 0
  399. if let currentIndex = self.viewerMedia?.currentIndex, let metadatas = self.viewerMedia?.metadatas, let ncplayer = self.ncplayer {
  400. if currentIndex == metadatas.count - 1 {
  401. index = 0
  402. } else {
  403. index = currentIndex + 1
  404. }
  405. self.viewerMedia?.goTo(index: index, direction: .forward, autoPlay: ncplayer.isPlay())
  406. }
  407. }
  408. func backward() {
  409. var index: Int = 0
  410. if let currentIndex = self.viewerMedia?.currentIndex, let metadatas = self.viewerMedia?.metadatas, let ncplayer = self.ncplayer {
  411. if currentIndex == 0 {
  412. index = metadatas.count - 1
  413. } else {
  414. index = currentIndex - 1
  415. }
  416. self.viewerMedia?.goTo(index: index, direction: .reverse, autoPlay: ncplayer.isPlay())
  417. }
  418. }
  419. }
  420. extension NCPlayerToolBar: AVPictureInPictureControllerDelegate {
  421. func pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
  422. guard let metadata = self.metadata else { return }
  423. guard let ncplayer = self.ncplayer else { return }
  424. if !ncplayer.isPlay() {
  425. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId":metadata.ocId, "enableTimerAutoHide": false])
  426. }
  427. }
  428. }