Already achieved most of the work hopefully but still struggling in this part.
I want to get playback milliseconds. With this code. i get the duration
and the current playing time
. But i also want the milliseconds as a part of my subtitle application.
Okay so we start with the seconds. this code gives me what i want:
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 the millisecond here aswell?
return (hours, minutes, seconds)
}
Now with this function. we format the time and calculate:
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 calling this function gets us what we want:
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
self.statics.text = "Current time: \(formatTimeFor(seconds: playhead)) ---> Full: \(formatTimeFor(seconds: duration))"
}
}
Thanks. your help is truly appreciated
Update
The current way of checking for my playback duration is to have a timer that hits a function every 0.5 seconds. like this:
func scheduledTimerWithTimeInterval(){
// Scheduling timer to Call the function "updateCounting" with the interval of 1 seconds
timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.updateCounting), userInfo: nil, repeats: true)
}
Side note. I do get the milliseconds in a way:
let millisecondsInt = (seconds.truncatingRemainder(dividingBy: 1) * 1000)
The problem is. at every 0.5 seconds the update_time()
function is hit. that gives me corrupt info. for example. if i wanted to put a subtitle at: 00:16:267
there's a 10% chance the code will run cause the numbers are random.
I assume that this code is somehow not correct, I'd like to have the counter
do the job this way:
Current time: 00:14:266 ---> Full: 04:12:610
Current time: 00:14:267 ---> Full: 04:12:610 // 267 milliseconds +1
Current time: 00:14:268 ---> Full: 04:12:610
Current time: 00:14:269 ---> Full: 04:12:610
Current time: 00:14:270 ---> Full: 04:12:610
Current time: 00:14:271 ---> Full: 04:12:610
Current time: 00:14:272 ---> Full: 04:12:610
Not this random number jumping way:
Current time: 00:13:767 ---> Full: 04:12:610
Current time: 00:14:267 ---> Full: 04:12:610
Current time: 00:14:767 ---> Full: 04:12:610
Current time: 00:15:266 ---> Full: 04:12:610
Current time: 00:15:767 ---> Full: 04:12:610
Current time: 00:16:267 ---> Full: 04:12:610
Current time: 00:16:767 ---> Full: 04:12:610