ghk*_*ren 5 iphone avfoundation ios avasset
我在 Mac 播放器中有视频,视频时长为 31 秒。当我在我的应用程序中使用它并加载该文件时,AVAsset 的持续时间为“28.03”。
AVAsset *videoAsset = [AVAsset assetWithURL:videoUrl];
Float64 time = CMTimeGetSeconds(videoAsset.duration);
Run Code Online (Sandbox Code Playgroud)
对于某些类型的资产,持续时间是近似值。如果您需要确切的持续时间(应该是极端情况),请使用:
NSDictionary *options = @{AVURLAssetPreferPreciseDurationAndTimingKey: @YES};
AVURLAsset *videoAsset = [URLAssetWithURL:videoUrl options:options];
Run Code Online (Sandbox Code Playgroud)
您可以在文档中找到更多信息。计算持续时间可能需要一些时间,因此请记住使用异步加载:
[videoAsset loadValuesAsynchronouslyForKeys:@[@"duration"] completionHandler:^{
switch ([videoAsset statusOfValueForKey:@"duration" error:nil]) {
case AVKeyValueStatusLoaded:
Float64 time = CMTimeGetSeconds(videoAsset.duration);
// ...
break;
default:
// other cases like cancellation or fail
break;
}
}];
Run Code Online (Sandbox Code Playgroud)
AVFoundation您可以在视频“发现 AV Foundation - WWDC 2010 Session 405”中找到有关使用 API 的更多技巧
| 归档时间: |
|
| 查看次数: |
2801 次 |
| 最近记录: |