IOS:AVMetadataItem
lastmod 2014-03-18created 2014-03-142 edits
An AVMetadataItem object represents an item of metadata associated with an audiovisual asset or with one of its tracks. To create metadata items for your own assets, you use the mutable subclass, AVMutableMetadataItem.
Extracting MP3 Album artwork
- Stackoverflow: Extracting MP3 Album artwork in iOS
- Stackoverflow: iOS AVFoundation: How do I fetch artwork from an mp3 file?
The way to extract and display the album artwork of an MP3 file is shown below.
- (void)loadImage:(MPMediaItem*)item
{
if (item == nil) {
return;
}
NSURL * songUrl = [item valueForProperty:MPMediaItemPropertyAssetURL];
// NSString *filePath = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"mp3"];
// NSURL *fileURL = [NSURL fileURLWithPath:filePath];
AVAsset *asset = [AVURLAsset URLAssetWithURL:songUrl options:nil];
NSArray *keys = [NSArray arrayWithObjects:@"commonMetadata", nil];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
NSArray *artworks = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata
withKey:AVMetadataCommonKeyArtwork
keySpace:AVMetadataKeySpaceCommon];
UIImage * img = nil;
for (AVMetadataItem *metadata in artworks) {
if ([metadata.keySpace isEqualToString:AVMetadataKeySpaceID3]) {
NSDictionary *dict = [metadata.value copyWithZone:nil];
img = [UIImage imageWithData:[dict objectForKey:@"data"]];
} else if ([metadata.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
img = [UIImage imageWithData:[metadata.value copyWithZone:nil]];
}
}
[self performSelectorOnMainThread:@selector(setArtwork:) withObject:img waitUntilDone:NO];
}];
}
- (void)setArtwork:(UIImage*)img
{
if (img != nil) {
self.selectAblumImageView.image = img;
} else {
self.selectAblumImageView.image = [UIImage imageNamed:@"icon_music_listdefault.png"];
}
}