123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- import Foundation
- import NCCommunication
- 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 var player: NCPlayer!
- private var wasInPlay: Bool = false
- private var playbackSliderEvent: sliderEventType = .ended
- private let seekDuration: Float64 = 15
-
- override func awakeFromNib() {
- super.awakeFromNib()
-
-
- 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.stringFromTimeInterval(interval: 0)
- labelCurrentTime.textColor = .lightGray
- labelOverallDuration.text = NCUtility.shared.stringFromTimeInterval(interval: 0)
- 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")
- }
-
- func setBarPlayer(player: NCPlayer) {
-
- self.player = player
-
- playbackSlider.value = 0
- playbackSlider.minimumValue = 0
- playbackSlider.maximumValue = Float(player.getVideoDurationSeconds())
- playbackSlider.addTarget(self, action: #selector(onSliderValChanged(slider:event:)), for: .valueChanged)
- labelCurrentTime.text = NCUtility.shared.stringFromTimeInterval(interval: 0)
- labelOverallDuration.text = "-" + NCUtility.shared.stringFromTimeInterval(interval:player.getVideoDurationSeconds())
-
- updateToolBar()
-
- player.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, preferredTimescale: 1), queue: .main, using: { (CMTime) in
-
- if player.currentItem?.status == .readyToPlay {
- if self.isHidden == false {
- self.updateToolBar()
- }
- }
- })
- }
-
- @objc public func hideToolBar() {
-
- updateToolBar()
-
- UIView.animate(withDuration: 0.3, animations: {
- self.alpha = 0
- }, completion: { (value: Bool) in
- self.isHidden = true
- })
- }
-
- @discardableResult
- @objc public func showToolBar(metadata: tableMetadata) -> Bool {
-
- if !self.isHidden { return false}
- if metadata.livePhoto { return false}
- if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
-
- updateToolBar()
-
- UIView.animate(withDuration: 0.3, animations: {
- self.alpha = 1
- }, completion: { (value: Bool) in
- self.isHidden = false
- })
-
- return true
-
- } else {
-
- return false
- }
- }
-
- public func updateToolBar() {
- var namedPlay = "play.fill"
- if player.rate == 1 { namedPlay = "pause.fill"}
- let currentSeconds = player.getVideoCurrentSeconds()
- let durationSeconds = player.getVideoDurationSeconds()
-
- playbackSlider.value = Float(currentSeconds)
- playbackSlider.isEnabled = true
-
- backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.15", color: .white), 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), for: .normal)
- }
- playButton.isEnabled = true
-
- forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.15", color: .white), 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.stringFromTimeInterval(interval: currentSeconds)
- labelOverallDuration.text = "-" + NCUtility.shared.stringFromTimeInterval(interval: durationSeconds - currentSeconds)
- }
-
-
-
- @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: 1)
-
- switch touchEvent.phase {
- case .began:
- wasInPlay = player.rate == 1 ? true : false
- player.videoPause()
- playbackSliderEvent = .began
- case .moved:
- player.videoSeek(time: targetTime)
- playbackSliderEvent = .moved
- case .ended:
- player.videoSeek(time: targetTime)
- if wasInPlay {
- player.videoPlay()
- }
- playbackSliderEvent = .ended
- default:
- break
- }
- }
- }
-
- @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
- hideToolBar()
- }
-
-
-
- @IBAction func buttonTouchInside(_ sender: UIButton) {
- hideToolBar()
- }
-
- @IBAction func playerPause(_ sender: Any) {
-
- if player.timeControlStatus == .playing {
- player.videoPause()
- } else if player.timeControlStatus == .paused {
- player.videoPlay()
- }
- }
-
- @IBAction func setMute(_ sender: Any) {
-
- let mute = CCUtility.getAudioMute()
-
- CCUtility.setAudioMute(!mute)
- player.isMuted = !mute
- updateToolBar()
- }
-
- @IBAction func forwardButtonSec(_ sender: Any) {
-
- let playerCurrentTime = CMTimeGetSeconds(player.currentTime())
- let newTime = playerCurrentTime + seekDuration
-
- if newTime < player.getVideoDurationSeconds() {
- let time: CMTime = CMTimeMake(value: Int64(newTime * 1000 as Float64), timescale: 1000)
- player.videoSeek(time: time)
- }
- }
-
- @IBAction func backButtonSec(_ sender: Any) {
- let playerCurrenTime = CMTimeGetSeconds(player.currentTime())
- var newTime = playerCurrenTime - seekDuration
-
- if newTime < 0 { newTime = 0 }
- let time: CMTime = CMTimeMake(value: Int64(newTime * 1000 as Float64), timescale: 1000)
-
- player.videoSeek(time: time)
- }
- }
|