123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- //
- // NCPlayerToolBar.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 01/07/21.
- // Copyright © 2021 Marino Faggiana. All rights reserved.
- //
- // Author Marino Faggiana <marino.faggiana@nextcloud.com>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- import Foundation
- import NCCommunication
- import CoreMedia
- class NCPlayerToolBar: UIView {
-
- @IBOutlet weak var playButton: UIButton!
- @IBOutlet weak var muteButton: UIButton!
- @IBOutlet weak var forwardButton: UIButton!
- @IBOutlet weak var backButton: UIButton!
- @IBOutlet weak var playbackSlider: UISlider!
- @IBOutlet weak var labelOverallDuration: UILabel!
- @IBOutlet weak var labelCurrentTime: UILabel!
-
- enum sliderEventType {
- case began
- case ended
- case moved
- }
-
- private let appDelegate = UIApplication.shared.delegate as! AppDelegate
- private var ncplayer: NCPlayer?
- private var wasInPlay: Bool = false
- private var playbackSliderEvent: sliderEventType = .ended
- private let timeToAdd: CMTime = CMTimeMakeWithSeconds(15, preferredTimescale: 1)
- private var durationTime: CMTime = .zero
- private var timeObserver: Any?
- private var timerAutoHide: Timer?
- private var metadata: tableMetadata?
- // MARK: - View Life Cycle
- override func awakeFromNib() {
- super.awakeFromNib()
-
- // for disable gesture of UIPageViewController
- let panRecognizer = UIPanGestureRecognizer(target: self, action: nil)
- addGestureRecognizer(panRecognizer)
- let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSingleTapWith(gestureRecognizer:)))
- addGestureRecognizer(singleTapGestureRecognizer)
-
- let blurEffect = UIBlurEffect(style: .dark)
- let blurEffectView = UIVisualEffectView(effect: blurEffect)
-
- self.layer.cornerRadius = 15
- self.layer.masksToBounds = true
-
- blurEffectView.frame = self.bounds
- blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
- self.insertSubview(blurEffectView, at:0)
-
- playbackSlider.value = 0
- playbackSlider.minimumValue = 0
- playbackSlider.maximumValue = 0
- playbackSlider.isContinuous = true
- playbackSlider.tintColor = .lightGray
-
- labelCurrentTime.text = NCUtility.shared.stringFromTime(.zero)
- labelCurrentTime.textColor = .lightGray
- labelOverallDuration.text = NCUtility.shared.stringFromTime(.zero)
- labelOverallDuration.textColor = .lightGray
-
- backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.15", color: .lightGray), for: .normal)
- playButton.setImage(NCUtility.shared.loadImage(named: "play.fill", color: .lightGray), for: .normal)
- forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.15", color: .lightGray), for: .normal)
- muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .lightGray), for: .normal)
- }
-
- deinit {
- print("deinit NCPlayerToolBar")
-
- if self.timeObserver != nil {
- appDelegate.player?.removeTimeObserver(self.timeObserver!)
- }
- }
-
- func setBarPlayer(ncplayer: NCPlayer, timeSeek: CMTime, metadata: tableMetadata?) {
-
- self.ncplayer = ncplayer
- self.metadata = metadata
-
- if let durationTime = NCManageDatabase.shared.getVideoDurationTime(metadata: ncplayer.metadata) {
-
- self.durationTime = durationTime
-
- playbackSlider.value = 0
- playbackSlider.minimumValue = 0
- playbackSlider.maximumValue = Float(durationTime.value)
- playbackSlider.addTarget(self, action: #selector(onSliderValChanged(slider:event:)), for: .valueChanged)
- labelCurrentTime.text = NCUtility.shared.stringFromTime(.zero)
- labelOverallDuration.text = "-" + NCUtility.shared.stringFromTime(durationTime)
- }
- updateToolBar(timeSeek: timeSeek)
-
- self.timeObserver = appDelegate.player?.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, preferredTimescale: 1), queue: .main, using: { (CMTime) in
-
- if self.appDelegate.player?.currentItem?.status == .readyToPlay {
- if self.isHidden == false {
- self.updateToolBar()
- }
- }
- })
- }
-
- public func hideToolBar() {
-
- UIView.animate(withDuration: 0.3, animations: {
- self.alpha = 0
- }, completion: { (value: Bool) in
- self.isHidden = true
- })
- }
-
- @objc private func automaticHideToolBar() {
-
- if let metadata = self.metadata {
- NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterHidePlayerToolBar, userInfo: ["ocId":metadata.ocId])
- }
- }
-
- public func showToolBar(metadata: tableMetadata, detailView: NCViewerMediaDetailView?) {
-
- if metadata.classFile != NCCommunicationCommon.typeClassFile.video.rawValue && metadata.classFile != NCCommunicationCommon.typeClassFile.audio.rawValue { return }
- if metadata.livePhoto { return }
-
- timerAutoHide?.invalidate()
- timerAutoHide = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(automaticHideToolBar), userInfo: nil, repeats: false)
-
- if !self.isHidden { return }
- if let detailView = detailView {
- if detailView.isShow() { return }
- }
-
- updateToolBar()
-
- UIView.animate(withDuration: 0.3, animations: {
- self.alpha = 1
- }, completion: { (value: Bool) in
- self.isHidden = false
- })
- }
-
- public func updateToolBar(timeSeek: CMTime? = nil) {
- var namedPlay = "play.fill"
- var currentTime = appDelegate.player?.currentTime() ?? .zero
- currentTime = currentTime.convertScale(1000, method: .default)
-
- if appDelegate.player?.rate == 1 { namedPlay = "pause.fill"}
-
- if timeSeek != nil {
- playbackSlider.value = Float(timeSeek!.value)
- } else {
- playbackSlider.value = Float(currentTime.value)
- }
- playbackSlider.isEnabled = true
-
- if #available(iOS 13.0, *) {
- backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.15", color: .white), for: .normal)
- } else {
- backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.15", color: .white, size: 30), for: .normal)
- }
- backButton.isEnabled = true
-
- if #available(iOS 13.0, *) {
- playButton.setImage(NCUtility.shared.loadImage(named: namedPlay, color: .white, symbolConfiguration: UIImage.SymbolConfiguration(pointSize: 30)), for: .normal)
- } else {
- playButton.setImage(NCUtility.shared.loadImage(named: namedPlay, color: .white, size: 30), for: .normal)
- }
- playButton.isEnabled = true
-
- if #available(iOS 13.0, *) {
- forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.15", color: .white), for: .normal)
- } else {
- forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.15", color: .white, size: 30), for: .normal)
- }
- forwardButton.isEnabled = true
-
- if CCUtility.getAudioMute() {
- muteButton.setImage(NCUtility.shared.loadImage(named: "audioOff", color: .white), for: .normal)
- } else {
- muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
- }
- muteButton.isEnabled = true
-
- labelCurrentTime.text = NCUtility.shared.stringFromTime(currentTime)
- labelOverallDuration.text = "-" + NCUtility.shared.stringFromTime(self.durationTime - currentTime)
- }
-
- //MARK: - Event / Gesture
-
- @objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
-
- if let touchEvent = event.allTouches?.first {
-
- let seconds: Int64 = Int64(self.playbackSlider.value)
- let targetTime: CMTime = CMTimeMake(value: seconds, timescale: 1000)
-
- switch touchEvent.phase {
- case .began:
- wasInPlay = appDelegate.player?.rate == 1 ? true : false
- ncplayer?.videoPause()
- playbackSliderEvent = .began
- case .moved:
- ncplayer?.videoSeek(time: targetTime)
- playbackSliderEvent = .moved
- case .ended:
- ncplayer?.videoSeek(time: targetTime)
- if wasInPlay {
- ncplayer?.videoPlay()
- }
- playbackSliderEvent = .ended
- default:
- break
- }
- }
- }
-
- @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
-
- hideToolBar()
- }
-
- //MARK: - Action
-
- @IBAction func buttonTouchInside(_ sender: UIButton) {
- }
-
- @IBAction func playerPause(_ sender: Any) {
-
- if appDelegate.player?.timeControlStatus == .playing {
- ncplayer?.videoPause()
- if let time = appDelegate.player?.currentTime() {
- ncplayer?.saveTime(time)
- }
- } else if appDelegate.player?.timeControlStatus == .paused {
- ncplayer?.videoPlay()
- } else if appDelegate.player?.timeControlStatus == .waitingToPlayAtSpecifiedRate {
- print("timeControlStatus.waitingToPlayAtSpecifiedRate")
- if let reason = appDelegate.player?.reasonForWaitingToPlay {
- switch reason {
- case .evaluatingBufferingRate:
- print("reasonForWaitingToPlay.evaluatingBufferingRate")
- case .toMinimizeStalls:
- print("reasonForWaitingToPlay.toMinimizeStalls")
- case .noItemToPlay:
- print("reasonForWaitingToPlay.noItemToPlay")
- default:
- print("Unknown \(reason)")
- }
- }
- }
- }
-
- @IBAction func setMute(_ sender: Any) {
-
- let mute = CCUtility.getAudioMute()
-
- CCUtility.setAudioMute(!mute)
- appDelegate.player?.isMuted = !mute
- updateToolBar()
- }
-
- @IBAction func forwardButtonSec(_ sender: Any) {
- guard let ncplayer = ncplayer else { return }
- guard let player = appDelegate.player else { return }
-
- let currentTime = player.currentTime()
- let newTime = CMTimeAdd(currentTime, timeToAdd)
-
- if newTime < durationTime {
- ncplayer.videoSeek(time: newTime)
- } else if newTime >= durationTime {
- ncplayer.videoSeek(time: .zero)
- }
- }
-
- @IBAction func backButtonSec(_ sender: Any) {
- guard let ncplayer = ncplayer else { return }
- guard let player = appDelegate.player else { return }
-
- let currentTime = player.currentTime()
- let newTime = CMTimeSubtract(currentTime, timeToAdd)
-
- ncplayer.videoSeek(time: newTime)
- }
- }
|