親愛的程序猿們,節(jié)日快樂!K蕉拧船惨!
CMTime定義是一個C語言的結(jié)構(gòu)體,使用雙精度double類型表示時間,比NSTimeInterval浮點型數(shù)據(jù)精度要高萍丐。
typedef struct
{
CMTimeValue value; // 分子
CMTimeScale timescale;// 分母
CMTimeFlags flags;// 位掩碼 kCMTimeFlags_Valid kCMTimeFlags_PositiveInfinity(表示時間的指定狀態(tài)株憾,判斷時間是否有效凑保、不確定和是否出現(xiàn)舍入值)
CMTimeEpoch epoch;// Flag bits for a CMTime
} CMTime;
Core Media通過這種方式合理的表示時間值饿敲。
CMTime 創(chuàng)建
CMTime time1 = CMTimeMake(3, 1);//對于音頻時間可能存在對應(yīng)的timescale不能寫固定值1
CMTime time2 = CMTimeMake(6, 1);
// 打印CMTime
CMTimeShow(time1);//{3/1 = 3.000}
CMTimeShow(time2);//{6/1 = 6.000}
// 方法傳入的CMTime時間對應(yīng)的一個Value和這個時間的timescale
CMTime time = CMTimeMakeWithSeconds(10, 2);
CMTimeShow(time);//{20/2 = 10.000}得到的是10.000
CMTime的計算妻导、比較
相加
CMTime addTime = CMTimeAdd(time1, time2);
CMTimeShow(addTime);//{9/1 = 9.000}
相減
CMTime subTime = CMTimeSubtract(time2, time1);
CMTimeShow(subTime);//{3/1 = 3.000}
相乘
CMTime mulTime1 = CMTimeMultiply(time1, 2);
CMTime mulTime2 = CMTimeMultiplyByFloat64(time1, 1.5);
CMTime mulTime3 = CMTimeMultiplyByRatio(time1, 2, 3);
CMTimeShow(mulTime1);//{6/1 = 6.000}
CMTimeShow(mulTime2);//{4500000000/1000000000 = 4.500}
CMTimeShow(mulTime3);//{6/3 = 2.000}
比較、獲取最大最小時間
//CMTimeCompare(time1, time2) time1小于time2返回-1怀各,等于返回0倔韭,大于返回1,和CMTIMECOMPAREINLINE宏都是比較時間大小瓢对。
int32_t comResult = CMTimeCompare(time1, time2);
//return true if time1 <= time2.
BOOL comResult2 = CMTIME_COMPARE_INLINE(time1, <=, time2);
NSLog(@"%d %d",comResult,comResult2);// -1 1
CMTime minTime = CMTimeMinimum(time1, time2);
CMTime maxTime = CMTimeMaximum(time1, time2);
CMTimeShow(minTime);//{3/1 = 3.000}
CMTimeShow(maxTime);//{6/1 = 6.000}
CMTimeRange
typedef struct
{
CMTime start;// 起點
CMTime duration;// 持續(xù)時間
} CMTimeRange;
創(chuàng)建時間范圍
CMTimeRange timeRange1 = CMTimeRangeMake(time1, time2);
CMTimeRange timeRange2 = CMTimeRangeFromTimeToTime(time1, time2);
CMTimeRangeShow(timeRange1);//{{3/1 = 3.000}, {6/1 = 6.000}}
CMTimeRangeShow(timeRange2);//{{3/1 = 3.000}, {3/1 = 3.000}}
時間范圍的交集和并集
CMTimeRange intersectionRange = CMTimeRangeGetIntersection(timeRange1, timeRange2);
CMTimeRangeShow(intersectionRange);//{{3/1 = 3.000}, {3/1 = 3.000}}
CMTimeRange unionRange = CMTimeRangeGetUnion(timeRange1, timeRange2);
CMTimeRangeShow(unionRange);//{{3/1 = 3.000}, {6/1 = 6.000}}
CMTime endTime = CMTimeRangeGetEnd(timeRange2);
CMTimeShow(endTime);//{6/1 = 6.000}