【发布时间】:2012-03-09 20:20:08
【问题描述】:
这可能看起来很荒谬,但是如何在 Objective-C 中将 CMTime 的秒数输出到控制台?我只需要将该值除以时间刻度,然后以某种方式在控制台中看到它。
【问题讨论】:
标签: objective-c console cmtime
这可能看起来很荒谬,但是如何在 Objective-C 中将 CMTime 的秒数输出到控制台?我只需要将该值除以时间刻度,然后以某种方式在控制台中看到它。
【问题讨论】:
标签: objective-c console cmtime
NSLog(@"seconds = %f", CMTimeGetSeconds(cmTime));
【讨论】:
CMTime constants 之一(可能不是kCMTimeZero)。尝试使用CMTimeShow 看看它的字段是什么。
简单:
NSLog(@"%lld", time.value/time.timescale);
【讨论】:
如果你想转换成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);
【讨论】:
在此之前的所有答案都不处理 NaN 情况:
斯威夫特 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)
}
【讨论】:
如果您只想将CMTime 打印到控制台以进行调试,请使用CMTimeShow:
Objective-C
CMTime time = CMTimeMakeWithSeconds(2.0, 60000);
CMTimeShow(time);
斯威夫特
var time = CMTimeMakeWithSeconds(2.0, 60000)
CMTimeShow(time)
它将打印value、timescale并计算seconds:
{120000/6000 = 2.0}
【讨论】:
CMTime currentTime = audioPlayer.currentItem.currentTime;
float videoDurationSeconds = CMTimeGetSeconds(currentTime);
【讨论】: