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) } }
Brandon A May 03 '17 at 12:18 a.m. 2017-05-03 00:18
source share