Converting CMTime to human readable time in objective-c
You can use CMTimeCopyDescription
, it work really well.
NSString *timeDesc = (NSString *)CMTimeCopyDescription(NULL, self.player.currentTime);
NSLog(@"Description of currentTime: %@", timeDesc);
edit: okay, i read the question too fast, this is not what your wanted but could be helpful anyway for debuging.
edit: as @bcattle commented, the implementation i suggested contain a memory leak with ARC. Here the corrected version :
NSString *timeDesc = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, self.player.currentTime));
NSLog(@"Description of currentTime: %@", timeDesc);
You can use this as well to get a video duration in a text format if you dont require a date format
AVURLAsset *videoAVURLAsset = [AVURLAsset assetWithURL:url];
CMTime durationV = videoAVURLAsset.duration;
NSUInteger dTotalSeconds = CMTimeGetSeconds(durationV);
NSUInteger dHours = floor(dTotalSeconds / 3600);
NSUInteger dMinutes = floor(dTotalSeconds % 3600 / 60);
NSUInteger dSeconds = floor(dTotalSeconds % 3600 % 60);
NSString *videoDurationText = [NSString stringWithFormat:@"%i:%02i:%02i",dHours, dMinutes, dSeconds];
There is always an extension ;)
import CoreMedia
extension CMTime {
var durationText:String {
let totalSeconds = Int(CMTimeGetSeconds(self))
let hours:Int = Int(totalSeconds / 3600)
let minutes:Int = Int(totalSeconds % 3600 / 60)
let seconds:Int = Int((totalSeconds % 3600) % 60)
if hours > 0 {
return String(format: "%i:%02i:%02i", hours, minutes, seconds)
} else {
return String(format: "%02i:%02i", minutes, seconds)
}
}
}
to use
videoPlayer?.addPeriodicTimeObserverForInterval(CMTime(seconds: 1, preferredTimescale: 1), queue: dispatch_get_main_queue()) { time in
print(time.durationText)
}