2

i am developing an audio player. I successfully get other information of the mp3 file. but is unable to get the album art of the mp3 file. using this code i get the mp3 file info.

- (NSDictionary *)songID3Tags
{   
AudioFileID fileID = nil;
OSStatus error = noErr;
error = AudioFileOpenURL((CFURLRef)self.filePath, kAudioFileReadPermission, 0, &fileID);
    if (error != noErr) {
    NSLog(@"AudioFileOpenURL failed");
}
UInt32 id3DataSize  = 0;
    char *rawID3Tag    = NULL;

error = AudioFileGetPropertyInfo(fileID, kAudioFilePropertyID3Tag, &id3DataSize, NULL);
if (error != noErr)
    NSLog(@"AudioFileGetPropertyInfo failed for ID3 tag");

rawID3Tag = (char *)malloc(id3DataSize);
if (rawID3Tag == NULL)
    NSLog(@"could not allocate %lu bytes of memory for ID3 tag", id3DataSize);

error = AudioFileGetProperty(fileID, kAudioFilePropertyID3Tag, &id3DataSize, rawID3Tag);
if( error != noErr )
    NSLog(@"AudioFileGetPropertyID3Tag failed");

UInt32 id3TagSize = 0;
UInt32 id3TagSizeLength = 0;

error = AudioFormatGetProperty(kAudioFormatProperty_ID3TagSize, id3DataSize, rawID3Tag, &id3TagSizeLength, &id3TagSize);

if (error != noErr) {
    NSLog( @"AudioFormatGetProperty_ID3TagSize failed" );
    switch(error) {
        case kAudioFormatUnspecifiedError:
            NSLog( @"Error: audio format unspecified error" ); 
            break;
        case kAudioFormatUnsupportedPropertyError:
            NSLog( @"Error: audio format unsupported property error" ); 
            break;
        case kAudioFormatBadPropertySizeError:
            NSLog( @"Error: audio format bad property size error" ); 
            break;
        case kAudioFormatBadSpecifierSizeError:
            NSLog( @"Error: audio format bad specifier size error" ); 
            break;
        case kAudioFormatUnsupportedDataFormatError:
            NSLog( @"Error: audio format unsupported data format error" ); 
            break;
        case kAudioFormatUnknownFormatError:
            NSLog( @"Error: audio format unknown format error" ); 
            break;
        default:
            NSLog( @"Error: unknown audio format error" ); 
            break;
    }
}   

CFDictionaryRef piDict = nil;
UInt32 piDataSize = sizeof(piDict);

error = AudioFileGetProperty(fileID, kAudioFilePropertyInfoDictionary, &piDataSize, &piDict);
if (error != noErr)
    NSLog(@"AudioFileGetProperty failed for property info dictionary");

free(rawID3Tag);

return (NSDictionary*)piDict;
}

I know through kAudioFilePropertyAlbumArtwork i can get the album art of the mp3 file, but I do not know how to get it.

beryllium
  • 29,669
  • 15
  • 106
  • 125
Fahad Jamal
  • 341
  • 4
  • 17
  • If you are streaming audio then it's not possible to get artWork/thumbnail. Perhaps this link will help you. "http://stackoverflow.com/questions/9723449/mpmovieplayercontroller-getting-thumbnail-from-streaming-video" – Khalid Usman Apr 06 '12 at 08:21
  • No i do not have the streaming audio files. The audio files are already downloaded to the app. – Fahad Jamal Apr 06 '12 at 08:40
  • Try this http://stackoverflow.com/questions/6607401/get-album-artwork-from-id3-tag-convert-function-from-java-to-objective-c – Yomo710 Apr 06 '12 at 11:04

1 Answers1

4
    - (NSArray *)artworksForFileAtPath:(NSString *)path {
NSMutableArray *artworkImages = [NSMutableArray array];
NSURL *u = [NSURL fileURLWithPath:path];
AVURLAsset *a = [AVURLAsset URLAssetWithURL:u options:nil];
 NSArray *artworks = [AVMetadataItem metadataItemsFromArray:a.commonMetadata  withKey:AVMetadataCommonKeyArtwork keySpace:AVMetadataKeySpaceCommon];

for (AVMetadataItem *i in artworks)
{
    NSString *keySpace = i.keySpace;
    UIImage *im = nil;

    if ([keySpace isEqualToString:AVMetadataKeySpaceID3])
    {
        NSDictionary *d = [i.value copyWithZone:nil];
        im = [UIImage imageWithData:[d objectForKey:@"data"]];
    }
    else if ([keySpace isEqualToString:AVMetadataKeySpaceiTunes])
        im = [UIImage imageWithData:[i.value copyWithZone:nil]];

    if (im)
        [artworkImages addObject:im];
}
NSLog(@"array description is %@", [artworkImages description]);
return artworkImages; }

The Above code return the album_art of the mp3 file. Where path is the audio file path.

Fahad Jamal
  • 341
  • 4
  • 17
  • I've used like the above in iOS 7, but with iOS 8 it now causes a fatal crash. Specifically [d objectForKey:@"data"] causes an unrecognised selector exception. – Gerry Beauregard Sep 27 '14 at 12:41