CMTimeCompare是如何工作的?苹果似乎在他们的文档中省略了返回值。
我假设如果时间相等,则返回零,并根据哪个更大返回正数或负数1?CMTimeCompare是如何工作的?苹果似乎在他们的文档中省略了返回值。
我假设如果时间相等,则返回零,并根据哪个更大返回正数或负数1?从CMTime.h文件:
返回两个CMTimes的数字关系(-1 = less than,1 = greater than,0 = equal)。
如果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
这是用 AVPlayerItem
的 .currentTime()
和 .duration
在 Swift 5
中的非常简单的解释。
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 }
}
CMTimeCompare(a, b)
,它会返回 -1 如果 a < b 吗? - Dex