How Does CMTimeCompare Work?
Asked Answered
K

3

8

How does CMTimeCompare work? Apple seems to have left out the return values from their documentation.

https://developer.apple.com/library/mac/#documentation/CoreMedia/Reference/CMTime/Reference/reference.html

I assume if the times are equal it returns zero and return positive or negative 1 based on which is greater?

Kourtneykovac answered 9/3, 2012 at 10:14 Comment(0)
S
19

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.

Scevour answered 9/3, 2012 at 10:17 Comment(1)
If I do CMTimeCompare(a, b) it returns -1 if a < b?Kourtneykovac
S
4

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

Stearn answered 9/2, 2015 at 21:39 Comment(1)
Unfortunately, CMTIME_COMPARE_INLINE is not (yet) available in Swift.Renz
D
1

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 }
}
Diwan answered 22/6, 2020 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.