2012-02-18 40 views
46

這看起來很荒謬,但是我怎樣才能將CMTime的秒數輸出到Objective-C的控制檯?我只需要按時間分割的值,然後以某種方式在控制檯中看到它。CMTime秒輸出

回答

128
NSLog(@"seconds = %f", CMTimeGetSeconds(cmTime)); 
+2

當CMTimeGetSeconds返回NaN時,情況如何? – BUDDAx2 2014-11-27 19:45:35

+0

我的猜測是輸入是['CMTime'常量]之一(https://developer.apple.com/library/IOs/documentation/CoreMedia/Reference/CMTime/index.html#//apple_ref/doc/constant_group/Time_Constants)(可能不是'kCMTimeZero')。嘗試使用'CMTimeShow'來查看它的字段是什麼。 – 2014-11-28 05:30:37

+0

檢查「持續時間> 0」適用於我 – BUDDAx2 2014-11-28 11:54:43

12

簡單:

 NSLog(@"%lld", time.value/time.timescale); 
+0

完美的,就像一個魅力! – arik 2012-02-18 23:36:19

4

如果你想在hh:mm:ss格式轉換,那麼你可以使用這個

NSUInteger durationSeconds = (long)CMTimeGetSeconds(audioDuration); 
NSUInteger hours = floor(dTotalSeconds/3600); 
NSUInteger minutes = floor(durationSeconds % 3600/60); 
NSUInteger seconds = floor(durationSeconds % 3600 % 60); 
NSString *time = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", hours, minutes, seconds]; 
NSLog(@"Time|%@", time); 
0
CMTime currentTime = audioPlayer.currentItem.currentTime; 
float videoDurationSeconds = CMTimeGetSeconds(currentTime);