1

我在用户的文档目录中有几个 MP3 文件,我希望能够获取并显示 ID3 艺术品标签。我曾尝试使用 NSURLAsset,但我不确定如何将其转换为 UIImage 或 UIImageView!

谢谢!

4

1 回答 1

3

您必须使用AVAsset类:
例如:

NSString *mp3Path = [[NSBundle mainBundle] pathForResource:@"audio-file" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:mp3Path];

AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
for (NSString *format in [asset availableMetadataFormats]) {
    for (AVMetadataItem *item in [asset metadataForFormat:format]) {
        if ([[item commonKey] isEqualToString:@"artwork"]) {            
            NSData *data = [(NSDictionary *)[item value] objectForKey:@"data"];
            UIImageView *img = [[[UIImageView alloc] initWithImage:[UIImage imageWithData:data]] autorelease];
            [self.view addSubview:img];
            continue;
        }
        NSLog(@"%@", item);
    }
}
NSLog(@"> Duration = %.2f seconds", CMTimeGetSeconds(asset.duration));
于 2011-12-28T14:06:54.997 回答