CMTimeCompare如何运作? Apple似乎已经从他们的文档中遗漏了返回值。
我假设如果时间相等则返回零并返回正或负1,基于哪个更大?
答案 0 :(得分:18)
来自 CMTime.h :
返回数值关系(-1 =小于,1 =大于, 0 =相等)两个CMTime。
如果time1小于time2,则返回-1。如果它们相等则返回0。如果time1大于time2,则返回1。
修改强>
请注意:
无效CMTime被认为等于其他无效CMTime, 并且大于 任何其他CMTime。正无穷大被认为小于任何无效的CMTime, 等于自身,并且比任何其他CMTime都要大。考虑无限期CMTime 小于任何无效的CMTime,小于正无穷大,等于它自己, 并且比任何其他CMTime都要大。负无穷大被认为等于自身, 并且比任何其他CMTime少。
答案 1 :(得分:3)
对于比CMTimeCompare()
更容易阅读的替代方案,请考虑使用CMTIME_COMPARE_INLINE
macro。例如
CMTIME_COMPARE_INLINE(time1, <=, time2)
如果time1&lt; = time2 ,将返回true
答案 2 :(得分:0)
在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 }
}