Search code examples
iosavplayer

How do I get current playing time and total play time in AVPlayer?


Is it possible get playing time and total play time in AVPlayer? If yes, how can I do this?


Solution

  • You can access currently played item by using currentItem property:

    AVPlayerItem *currentItem = yourAVPlayer.currentItem;
    

    Then you can easily get the requested time values

    CMTime duration = currentItem.duration; //total time
    CMTime currentTime = currentItem.currentTime; //playing time
    

    Swift 5:

    if let currentItem = player.currentItem {
        let duration = CMTimeGetSeconds(currentItem.duration)
        let currentTime = CMTimeGetSeconds(currentItem.currentTime())
    
        print("Duration: \(duration) s")
        print("Current time: \(currentTime) s")
    }