CMTimeCompare 是如何工作的?Apple 似乎在他们的文档中遗漏了返回值。
我假设如果时间相等,它返回零并返回正数或负数 1,基于哪个更大?
CMTimeCompare 是如何工作的?Apple 似乎在他们的文档中遗漏了返回值。
我假设如果时间相等,它返回零并返回正数或负数 1,基于哪个更大?
从CMTime.h:
返回两个 CMTime 的数值关系(-1 = 小于,1 = 大于,0 = 相等)。
如果 time1 小于 time2,则返回 -1。如果它们相等,则返回 0。如果 time1 大于 time2,则返回 1。
编辑:
请注意:
无效 CMTimes 被认为等于其他无效 CMTimes,并且大于任何其他 CMTime。正无穷被认为小于任何无效的 CMTime,等于自身,并且大于任何其他 CMTime。不确定的 CMTime 被认为小于任何无效 CMTime,小于正无穷大,等于自身,并且大于任何其他 CMTime。负无穷被认为等于它自己,并且小于任何其他 CMTime。
对于比 更容易阅读的替代方案,请CMTimeCompare()
考虑使用CMTIME_COMPARE_INLINE
宏。例如
CMTIME_COMPARE_INLINE(time1, <=, time2)
如果 time1 <= time2 将返回 true
这是Swift 5
一个非常简单的解释,使用AVPlayerItem
和它的.currentTime()
和.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 }
}