3

I understand that mp3s sometimes contain album artwork (in my case, I'm working with podcast files). Is there a way in iOS to extract the image data from an mp3 file?

Mark Armstrong
  • 819
  • 6
  • 9
  • 2
    Related question: http://stackoverflow.com/questions/1239460/reading-mp3-information-using-objective-c – Cyrille Oct 18 '11 at 22:20

3 Answers3

9

MP3s, including podcasts, do often have embedded metadata, including artwork. The easiest way to get the embedded metadata (for MP3s not in the iTunes library) is through the AVAsset class. If you are trying to get metadata over a network use the AVURLAsset instead, but accessing the metadata is the same with either.

AVAsset has an NSArray property named commonMetadata which contains instances of the class AVMetadataItem. You can iterate over this array to find the element with the commonKey of(usually) "artwork" The value property of this AVMetadataItem should be an NSDictionary of data about the artwork. Within this dictionary you will find the key "data" which should contain data you can use to construct a UIImage. This is quite nested and this description is admittedly confusing. So here is a code example.

NSURL *url = <# url of resource here #>;
AVAsset *asset = [AVAsset assetWithURL:url];
for (AVMetadataItem *metadataItem in asset.commonMetadata) {
    if ([metadataItem.commonKey isEqualToString:@"artwork"]){
        NSDictionary *imageDataDictionary = (NSDictionary *)metadataItem.value;
        NSData *imageData = [imageDataDictionary objectForKey:@"data"];
        UIImage *image = [UIImage imageWithData:imageData];
         // Display this image on my UIImageView property imageView
        self.imageView.image = image;
    }
}

Again this is a very simple example of how to load from a local resource; If you are loading this over a network you should use an AVURLAsset. Also be careful of the blocking nature of this call.

NJones
  • 27,139
  • 8
  • 70
  • 88
  • +1 NSURL *fileURL1 = [NSURL fileURLWithPath:str]; where str is the path of file ..i stuck because i m using [NSURL URLWithString:str]..I posted..maybe help others:) – Sishu Mar 13 '14 at 10:29
0
-(UIImage *)getMP3Pic:(NSURL *)url
{
    AVAsset *asset = [AVAsset assetWithURL:url];
    for (AVMetadataItem *metadataItem in asset.commonMetadata) {
       if ([metadataItem.commonKey isEqualToString:@"artwork"]){
            return [UIImage imageWithData:(NSData *)metadataItem.value];
       }
    }
}
Mona
  • 5,939
  • 3
  • 28
  • 33
0

Take a look at Apple's sample AddMusic project and you'll see how they get the album artwork via MPMediaItem & MPMediaItemArtwork objects.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Thanks Michael but correct me if I'm wrong, my understanding is that MPMediaItems only refer to items in the user's iPod library. I am parsing a podcast's rss feed and streaming audio for a podcast the user has not subscribed to in iTunes. Any ideas? – Mark Armstrong Oct 20 '11 at 19:08
  • Ahh, for some reason I thought the podcast was already in your iTunes library. Let me go back to the drawing board. Right now, I can't imagine that artwork is somehow embedded in a .mp3 file. Maybe in the .m3u playlist or in the RSS feed, but in a .mp3 file? – Michael Dautermann Oct 20 '11 at 20:49