AVAsset
专辑一个抽象类,里面包含曲目、格式、时长, 以及元数据NSData(二进制的bytes).
@property (nonatomic, readonly) NSArray<AVAssetTrack *> *tracks;
加载格式
AVURLAsset *anAsset = [[AVURLAsset alloc] initWithURL:url options:nil];
NSArray *keys = @[@"duration",@"tracks"];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^() {
NSError *error = nil;
AVKeyValueStatus tracksStatus = [asset statusOfValueForKey:@"tracks" error:&error];
//根据相应的属性状态进行对应的处理
switch (tracksStatus) {
case AVKeyValueStatusUnknown:
//TODO
break;
case AVKeyValueStatusLoading:
//TODO
break;
case AVKeyValueStatusLoaded:
//TODO
break;
case AVKeyValueStatusFailed:
//TODO
break;
case AVKeyValueStatusCancelled:
//TODO
break;
}
}];
我们真正存储媒体资源的统一媒体类型。
NSArray<AVAssetTrack *> *audioAssetTracks = [asset tracksWithMediaType:AVMediaTypeAudio];
NSArray<AVAssetTrack *> *videoAssetTracks = [asset tracksWithMediaType:AVMediaTypeVideo];
- (instancetype)initWithAsset:(AVAsset *)asset NS_DESIGNATED_INITIALIZER;
//获取一张图片,requestedTime 指定要获取视频中哪个时刻的图片,actualTime 返回图片实际是视频的哪个时刻,outError 返回错误信息
- (nullable CGImageRef)copyCGImageAtTime:(CMTime)requestedTime actualTime:(nullable CMTime *)actualTime error:(NSError * _Nullable * _Nullable)outError CF_RETURNS_RETAINED;
//获取多张图片,每一次图片生成后,都会调用一次 handler
- (void)generateCGImagesAsynchronouslyForTimes:(NSArray<NSValue *> *)requestedTimes completionHandler:(AVAssetImageGeneratorCompletionHandler)handler;
//上述 handler 的类型如下,回调中的参数有图片的请求时刻和实际时刻,图片,状态(成功、失败、取消),错误信息
typedef void (^AVAssetImageGeneratorCompletionHandler)(CMTime requestedTime, CGImageRef _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error);