How does CMTimeCompare work? Apple seems to have left out the return values from their documentation.
I assume if the times are equal it returns zero and return positive or negative 1 based on which is greater?
How does CMTimeCompare work? Apple seems to have left out the return values from their documentation.
I assume if the times are equal it returns zero and return positive or negative 1 based on which is greater?
From CMTime.h:
Returns the numerical relationship (-1 = less than, 1 = greater than, 0 = equal) of two CMTimes.
-1 is returned if time1 is less than time2. 0 is returned if they are equal. 1 is returned if time1 is greater than time2.
EDIT:
Please note that:
Invalid CMTimes are considered to be equal to other invalid CMTimes, and greater than any other CMTime. Positive infinity is considered to be less than any invalid CMTime, equal to itself, and greater than any other CMTime. An indefinite CMTime is considered to be less than any invalid CMTime, less than positive infinity, equal to itself, and greater than any other CMTime. Negative infinity is considered to be equal to itself, and less than any other CMTime.
For an alternative that's much easier to read than CMTimeCompare()
, consider using the CMTIME_COMPARE_INLINE
macro. For example
CMTIME_COMPARE_INLINE(time1, <=, time2)
will return true if time1 <= time2
Here is it in Swift 5
with a very easy explanation using AVPlayerItem
and its .currentTime()
and .duration
let videoCurrentTime = playerItem.currentTime() // eg. the video is at the 30 sec point
let videoTotalDuration = playerItem.duration // eg. the video is 60 secs long in total
// 1. this will be false because if the videoCurrentTime is at 30 and it's being compared to the videoTotalDuration which is 60 then they are not equal.
if CMTimeCompare(videoCurrentTime, videoTotalDuration) == 0 { // 0 means equal
print("do something")
// the print statement will NOT run because this is NOT true
// it is the same thing as saying: if videoCurrentTime == videoCurrentTime { do something }
}
// 2. this will be true because if the videoCurrentTime is at 30 and it's being compared to the videoTotalDuration which is 60 then they are not equal.
if CMTimeCompare(videoCurrentTime, videoTotalDuration) != 0 { // != 0 means not equal
print("do something")
// the print statement WILL run because it IS true
// this is the same thing as saying: if videoCurrentTime != videoTotalDuration { do something }
}
// 3. this will be true because if the videoCurrentTime is at 30 and it's being compared to 0 then 30 is greater then 0
if CMTimeCompare(videoCurrentTime, .zero) == 1 { // 1 means greater than
print("do something")
// the print statement WILL run because it IS true
// this is the same thing as saying: if videoCurrentTime > 0 { do something }
}
// 4. this will be true because if the videoCurrentTime is at 30 and it's being compared to the videoTotalDuration which is 60 then 30 is less than 60
if CMTimeCompare(videoCurrentTime, videoTotalDuration) == -1 { // -1 means less than
print("do something")
// the print statement WILL run because it IS true
// this is the same thing as saying: if videoCurrentTime < videoTotalDuration { do something }
}
© 2022 - 2024 — McMap. All rights reserved.
CMTimeCompare(a, b)
it returns -1 if a < b? – Kourtneykovac