How to play video

state of player

AVPlayerItem.Status and AVPlayerItem classes provide information about current state of player:

  • check if there is any error
  • current playback time
  • duration of video
  • state of buffer
  • check is a player ready to play
Get information about player

You can observe these values using KVO.

Observe player events with KVO

current playback time

AVPlayer object allows to add the observer to observe a current play time. This is useful for changing the value of the slider that indicates the current playback time.

Don't forget to remove the observer when it's not needed.

observe a current play time

Use the current item object of player to get duration of video.

var duration: TimeInterval {
    get {
        guard let item = player?.currentItem else {
            return 0
        }            
        return  CMTimeGetSeconds(item.duration)
    }
}