CMTimeCompare是如何工作的?

8
3个回答

19

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(a, b),它会返回 -1 如果 a < b 吗? - Dex

4

如果您希望一种比CMTimeCompare()更易读的替代方案,请考虑使用CMTIME_COMPARE_INLINE。例如:

CMTIME_COMPARE_INLINE(time1, <=, time2)

如果 time1 <= time2,则返回 true


很遗憾,CMTIME_COMPARE_INLINE在Swift中尚不可用。 - adriaan

1

这是用 AVPlayerItem.currentTime().durationSwift 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 }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接