arik
arik

Reputation: 29240

CMTime seconds output

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.

Upvotes: 64

Views: 42733

Answers (6)

Alexander Volkov
Alexander Volkov

Reputation: 8372

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)
}

Upvotes: 2

Fantini
Fantini

Reputation: 2077

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}

Upvotes: 1

amisha.beladiya
amisha.beladiya

Reputation: 363

 CMTime currentTime = audioPlayer.currentItem.currentTime;
 float videoDurationSeconds = CMTimeGetSeconds(currentTime);

Upvotes: 0

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39978

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);

Upvotes: 6

rob mayoff
rob mayoff

Reputation: 385500

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

Upvotes: 161

0xDE4E15B
0xDE4E15B

Reputation: 1294

Simple:

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

Upvotes: 16

Related Questions