83
votes

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

10

10 Answers

170
votes

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
23
votes
_audioPlayer = [self playerWithAudio:_audio];
_observer =
[_audioPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, 2)
                                           queue:dispatch_get_main_queue()
                                      usingBlock:^(CMTime time)
                                      {
                                          _progress = CMTimeGetSeconds(time);
                                      }];
15
votes

Swift 3

let currentTime:Double = player.currentItem.currentTime().seconds

You can get the seconds of your current time by accessing the seconds property of the currentTime(). This will return a Double that represents the seconds in time. Then you can use this value to construct a readable time to present to your user.

First, include a method to return the time variables for H:mm:ss that you will display to the user:

func getHoursMinutesSecondsFrom(seconds: Double) -> (hours: Int, minutes: Int, seconds: Int) {
    let secs = Int(seconds)
    let hours = secs / 3600
    let minutes = (secs % 3600) / 60
    let seconds = (secs % 3600) % 60
    return (hours, minutes, seconds)
}

Next, a method that will convert the values you retrieved above into a readable string:

func formatTimeFor(seconds: Double) -> String {
    let result = getHoursMinutesSecondsFrom(seconds: seconds)
    let hoursString = "\(result.hours)"
    var minutesString = "\(result.minutes)"
    if minutesString.characters.count == 1 {
        minutesString = "0\(result.minutes)"
    }
    var secondsString = "\(result.seconds)"
    if secondsString.characters.count == 1 {
        secondsString = "0\(result.seconds)"
    }
    var time = "\(hoursString):"
    if result.hours >= 1 {
        time.append("\(minutesString):\(secondsString)")
    }
    else {
        time = "\(minutesString):\(secondsString)"
    }
    return time
}

Now, update the UI with the previous calculations:

func updateTime() {
    // Access current item
    if let currentItem = player.currentItem {
        // Get the current time in seconds
        let playhead = currentItem.currentTime().seconds
        let duration = currentItem.duration.seconds
        // Format seconds for human readable string
        playheadLabel.text = formatTimeFor(seconds: playhead)
        durationLabel.text = formatTimeFor(seconds: duration)
    }
}
15
votes

With Swift 4.2, use this;

let currentPlayer = AVPlayer()
if let currentItem = currentPlayer.currentItem {
    let duration = currentItem.asset.duration
}
let currentTime = currentPlayer.currentTime()
9
votes

Swift 4

    self.playerItem = AVPlayerItem(url: videoUrl!)
    self.player = AVPlayer(playerItem: self.playerItem)

    self.player?.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 1), queue: DispatchQueue.main, using: { (time) in
        if self.player!.currentItem?.status == .readyToPlay {
            let currentTime = CMTimeGetSeconds(self.player!.currentTime())

            let secs = Int(currentTime)
            self.timeLabel.text = NSString(format: "%02d:%02d", secs/60, secs%60) as String//"\(secs/60):\(secs%60)"

    })
}
5
votes
     AVPlayerItem *currentItem = player.currentItem;
     NSTimeInterval currentTime = CMTimeGetSeconds(currentItem.currentTime);
     NSLog(@" Capturing Time :%f ",currentTime);
5
votes

Swift:

let currentItem = yourAVPlayer.currentItem

let duration = currentItem.asset.duration
var currentTime = currentItem.asset.currentTime
4
votes

Swift 5: Timer.scheduledTimer seems better than addPeriodicTimeObserver if you want to have a smooth progress bar

static public var currenTime = 0.0
static public var currenTimeString = "00:00"

        Timer.scheduledTimer(withTimeInterval: 1/60, repeats: true) { timer in

            if self.player!.currentItem?.status == .readyToPlay {

                let timeElapsed = CMTimeGetSeconds(self.player!.currentTime())
                let secs = Int(timeElapsed)
                self.currenTime = timeElapsed
                self.currenTimeString = NSString(format: "%02d:%02d", secs/60, secs%60) as String


                print("AudioPlayer TIME UPDATE: \(self.currenTime)    \(self.currenTimeString)")
            }
        }
1
votes

Swift 4.2:

let currentItem = yourAVPlayer.currentItem
let duration = currentItem.asset.duration
let currentTime = currentItem.currentTime()
1
votes

in swift 5+

You can query the player directly to find the current time of the actively playing AVPlayerItem. The time is stored in a CMTime Struct for ease of conversion to various scales such as 10th of sec, 100th of a sec etc In most cases we need to represent times in seconds so the following will show you what you want

let currentTimeInSecs = CMTimeGetSeconds(player.currentTime())