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

IosAvplayer

Ios Problem Overview


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

Ios Solutions


Solution 1 - Ios

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

Solution 2 - Ios

_audioPlayer = [self playerWithAudio:_audio];
_observer =
[_audioPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, 2)
                                           queue:dispatch_get_main_queue()
                                      usingBlock:^(CMTime time)
                                      {
                                          _progress = CMTimeGetSeconds(time);
                                      }];

Solution 3 - Ios

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)
    }
}

Solution 4 - Ios

With Swift 4.2, use this;

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

Solution 5 - Ios

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)"

    })
}

Solution 6 - Ios

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

Solution 7 - Ios

Swift:

let currentItem = yourAVPlayer.currentItem

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

Solution 8 - Ios

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)")
            }
        }

Solution 9 - Ios

Swift 4.2:

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

Solution 10 - Ios

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())

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVoloda2View Question on Stackoverflow
Solution 1 - IosBartosz CiechanowskiView Answer on Stackoverflow
Solution 2 - IosTim KozakView Answer on Stackoverflow
Solution 3 - IosBrandon AView Answer on Stackoverflow
Solution 4 - IosKemal Can KaynakView Answer on Stackoverflow
Solution 5 - IosxuzepeiView Answer on Stackoverflow
Solution 6 - IosAthul RajView Answer on Stackoverflow
Solution 7 - IosUnis BarakatView Answer on Stackoverflow
Solution 8 - IosThyseliusView Answer on Stackoverflow
Solution 9 - IosMatthijsView Answer on Stackoverflow
Solution 10 - IosUmar CheemaView Answer on Stackoverflow