This may seem ridiculous, but how can I output the seconds of CMTime to the console in Objective-C? I simply need the value divided by the timescale and then somehow see it in the console.
Asked
Active
Viewed 4.2k times
6 Answers
161
NSLog(@"seconds = %f", CMTimeGetSeconds(cmTime));

rob mayoff
- 375,296
- 67
- 796
- 848
-
3how about that case when CMTimeGetSeconds returns NaN? – BUDDAx2 Nov 27 '14 at 19:45
-
My guess is the input is one of the [`CMTime` constants](https://developer.apple.com/library/IOs/documentation/CoreMedia/Reference/CMTime/index.html#//apple_ref/doc/constant_group/Time_Constants) (probably not `kCMTimeZero`). Try using `CMTimeShow` to see what its fields are. – rob mayoff Nov 28 '14 at 05:30
6
If you want to convert in hh:mm:ss
format then you can use this
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);

Inder Kumar Rathore
- 39,458
- 17
- 135
- 184
2
All answers before this one do not handle NaN case:
Swift 5:
/// Convert CMTime to TimeInterval
///
/// - Parameter time: CMTime
/// - Returns: TimeInterval
func cmTimeToSeconds(_ time: CMTime) -> TimeInterval? {
let seconds = CMTimeGetSeconds(time)
if seconds.isNaN {
return nil
}
return TimeInterval(seconds)
}

Alexander Volkov
- 7,904
- 1
- 47
- 44
1
If you just want to print a CMTime
to the console for debugging purposes use CMTimeShow
:
Objective-C
CMTime time = CMTimeMakeWithSeconds(2.0, 60000);
CMTimeShow(time);
Swift
var time = CMTimeMakeWithSeconds(2.0, 60000)
CMTimeShow(time)
It will print the value
, timescale
and calculate the seconds
:
{120000/6000 = 2.0}

Fantini
- 2,067
- 21
- 32
0
CMTime currentTime = audioPlayer.currentItem.currentTime;
float videoDurationSeconds = CMTimeGetSeconds(currentTime);

amisha.beladiya
- 363
- 1
- 12