您好我是客观C的新手。我使用AGImagePicker从手机库中选择图片和视频文件并在GridView中显示它们。但现在我想获得视频文件的持续时间,以显示视频缩略图。我不知道如何从ALAsset获取视频时长。有人请。任何类型的答案将不胜感激。
答案 0 :(得分:8)
1)在ALAssetPropertyDuration的帮助下获取媒体持续时间
double value = [[asset valueForProperty:ALAssetPropertyDuration] doubleValue]; //Find the Duraion
[cell.lblMediaLength setText:[self timeFormatted:value]];
2)将Total seconds转换为Hour:min:sec
的方法- (NSString *)timeFormatted:(double)totalSeconds
{
NSTimeInterval timeInterval = totalSeconds;
long seconds = lroundf(timeInterval); // Modulo (%) operator below needs int or long
int hour = 0;
int minute = seconds/60.0f;
int second = seconds % 60;
if (minute > 59) {
hour = minute/60;
minute = minute%60;
return [NSString stringWithFormat:@"%02d:%02d:%02d", hour, minute, second];
}
else{
return [NSString stringWithFormat:@"%02d:%02d", minute, second];
}
}
对于Swift
func timeFormatted(totalSeconds: Float64) -> String {
let timeInterval: NSTimeInterval = totalSeconds
let seconds: Int = lround(timeInterval)
var hour: Int = 0
var minute: Int = Int(seconds/60)
let second: Int = seconds % 60
if minute > 59 {
hour = minute / 60
minute = minute % 60
return String(format: "%02d:%02d:%02d", hour, minute, second)
}
else {
return String(format: "%02d:%02d", minute, second)
}
}
答案 1 :(得分:2)
如果asset
是ALAsset
:
NSNumber *number = [asset valueForProperty:ALAssetPropertyDuration];
这将返回NSNumber。如果你想要值:
double value = [[asset valueForProperty:ALAssetPropertyDuration] doubleValue];
答案 2 :(得分:0)
以下是我如何获得持续时间。
AVAsset *testAsset = [AVAsset assetWithURL:url];
return testAsset.duration;