How to get current play time and total play time in AVPlayer?

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

+47
ios avplayer
Nov 18 '11 at 11:17
source share
6 answers

You can access the currently playing item using the 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 
+112
Nov 21 '11 at 8:28
source share
 _audioPlayer = [self playerWithAudio:_audio]; _observer = [_audioPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, 2) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) { _progress = CMTimeGetSeconds(time); }]; 
+19
Jun 08 '14 at 7:49
source share

With Swift 2.0 use this:

 let currentPlayerItem = AVPlayer.currentItem let duration = currentPlayerItem?.asset.duration var currentTime = AVPlayer.currentTime() 
+6
Jan 14 '16 at 12:26
source share
  AVPlayerItem *currentItem = player.currentItem; NSTimeInterval currentTime = CMTimeGetSeconds(currentItem.currentTime); NSLog(@" Capturing Time :%f ",currentTime); 
+5
Jul 06 '15 at 12:01
source share

Swift 3

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

You can get seconds of your current time by accessing the seconds currentTime() property. This will return a Double value that represents seconds. You can then use this value to create readable time for presentation to your user.

First, enable the time variable return method for H:mm:ss , which you show 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 converts the values ​​you received 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 user interface 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) } } 
+4
May 03 '17 at 12:18 a.m.
source share

Swift:

 let currentItem = yourAVPlayer.currentItem let duration = currentItem.asset.duration var currentTime = currentItem.asset.currentTime 
+3
Sep 09 '15 at 0:01
source share



All Articles