4

I am trying to create a video player with AVPlayer.Videos load from youtube. I want when video current time changes, my uiSlider and my time label can update. I am using "observeValueForKeyPath" method to catch "loadedTimeRanges" status from player item. Here is my code:

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        print("keyPath: \(keyPath)")
        let playerItem:AVPlayerItem = object as! AVPlayerItem
        if keyPath == "status" {
            if playerItem.status ==  AVPlayerItemStatus.ReadyToPlay{
                print("AVPlayerItemStatusReadyToPlay")
            } else if playerItem.status == AVPlayerItemStatus.Failed {
                print("AVPlayerItemStatusFailed")
            }
        } else if keyPath == "loadedTimeRanges" {
            let currentTime = playerItem.currentTime().seconds
            let totalDuration = playerItem.duration.seconds
            print("value: \(Float(currentTime/totalDuration))")
            self.monitoringPlayback(player.currentItem!)
            self.slider.setValue(Float(currentTime/totalDuration), animated: false)
        }
    }

    func monitoringPlayback(playerItem:AVPlayerItem) {
        let currentSecond:Double = playerItem.currentTime().seconds
        self.updateVideoSlider(currentSecond)
        let timeString = self.updateTime(playerItem.duration.seconds - currentSecond)
        print("time string: \(timeString)")
        self.lblTime.text = timeString
    }

However, the "observeValueForKeyPath" method is only called 20-22 times everytime. Please, check the log screenshot: https://gyazo.com/5ca57ba532689d83aea855ae41387f53 It's the first time I use this method, so maybe I didn't understand how it work. If anyone who know it, please tell me why. Thanks for reading my question.

Ashley
  • 499
  • 1
  • 6
  • 15
  • If you want to know the current playback time why don't you use the `currentTime` method on `AVPlayer` ? . Right now you are observing the time ranges that have been loaded for the item. Once you run out of chunks to load you stop getting the notification. – Warren Burton Feb 16 '16 at 17:06
  • But how can I know the time change event? – Ashley Feb 16 '16 at 17:36
  • From the docs `This property is not key-value observable; use addPeriodicTimeObserverForInterval:queue:usingBlock: or addBoundaryTimeObserverForTimes:queue:usingBlock: instead.` – Warren Burton Feb 16 '16 at 17:57
  • Thanks for your nice solution. I used addPeriodicTimeObserverForInterval:queue:usingBlock: and did success. – Ashley Feb 16 '16 at 23:10

1 Answers1

-1

it, easy

var rightCurrentTimer: UILabel = {

    let lbl = UILabel()
    lbl.text = "00:00"
    lbl.textColor = .white
    lbl.translatesAutoresizingMaskIntoConstraints = false
    lbl.font = UIFont.systemFont(ofSize: 11)
    return lbl

}()

let leftWhatTimePlayed:UILabel = {

    let lbl = UILabel()
    lbl.translatesAutoresizingMaskIntoConstraints = false
    lbl.textColor = .white
    lbl.text = "00:00"
    lbl.font = UIFont.systemFont(ofSize: 11)
    return lbl

}()

let interval = CMTime(value: 1, timescale: 1)

    player?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using: { (timeProgress) in

        let secondsPlay = CMTimeGetSeconds(timeProgress)
        let secondString = String(format:"%02d" , Int(secondsPlay) % 60 )
        let minutesString = String(format:"%02d", Int(secondsPlay) / 60)

        self.leftWhatTimePlayed.text = "\(minutesString):\(secondString)"

        if let duration = self.player?.currentItem?.duration {

            let totalSecond = CMTimeGetSeconds(duration)

            let whatSecondIsPlay = totalSecond - secondsPlay

            let secondsIsPlay = String(format:"%02d" , Int(whatSecondIsPlay) % 60)
            let minutesIsPlay = String(format: "%02d", Int(whatSecondIsPlay) / 60)

            self.rightCurrentTimer.text = "\(minutesIsPlay):\(secondsIsPlay)"
            self.videoLengthTimeSlider.value = Float(secondsPlay/totalSecond)

        }


    })
Anton Russia
  • 197
  • 1
  • 2