8

CMTimeCompareはどのように機能しますか?Appleは彼らのドキュメントから戻り値を省略しているようです。

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

時間が等しい場合はゼロを返し、正または負の1を返すと思いますが、どちらが大きいですか?

4

3 に答える 3

19

CMTime.hから:

2つのCMTimeの数値関係(-1 =より小さい、1 =より大きい、0 =等しい)を返します。

time1がtime2より小さい場合、-1が返されます。等しい場合は0が返されます。time1がtime2より大きい場合、1が返されます。

編集:

その点に注意してください:

無効なCMTimeは、他の無効なCMTimeと等しく、他のどのCMTimeよりも大きいと見なされます。正の無限大は、無効なCMTimeよりも小さく、それ自体と等しく、他のCMTimeよりも大きいと見なされます。不定のCMTimeは、無効なCMTimeよりも小さく、正の無限大よりも小さく、それ自体と等しく、他のCMTimeよりも大きいと見なされます。負の無限大はそれ自体と等しく、他のどのCMTimeよりも小さいと見なされます。

于 2012-03-09T10:17:37.813 に答える
4

よりもはるかに読みやすい代替方法については、マクロCMTimeCompare()の使用を検討してください。例えばCMTIME_COMPARE_INLINE

CMTIME_COMPARE_INLINE(time1, <=, time2)

time1<=time2の場合はtrueを返します

于 2015-02-09T21:39:11.927 に答える
1

これは、とを使用し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 }
}
于 2020-06-22T19:19:59.147 に答える