2012-04-06 70 views
2

我正在開發的音頻播放器的專輯封面。我成功獲取了mp3文件的其他信息。但無法獲取mp3文件的專輯封面。使用此代碼我得到的MP3文件信息。如何獲得MP3文件

- (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; 
} 

我知道通過kAudioFilePropertyAlbumArtwork我可以得到MP3文件的專輯封面,但我不知道如何得到它。

+0

如果是流式音頻那麼它是不可能得到的插圖/縮略圖。也許這個鏈接會幫助你。 「http://stackoverflow.com/questions/9723449/mpmovieplayercontroller-getting-thumbnail-from-streaming-video」 – 2012-04-06 08:21:38

+0

不,我沒有流式音頻文件。音頻文件已經下載到應用程序。 – 2012-04-06 08:40:17

+0

試試這個 http://stackoverflow.com/questions/6607401/get-album-artwork-from-id3-tag-convert-function-from-java-to-objective-c – Yomo710 2012-04-06 11:04:50

回答

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; } 

上面的代碼返回mp3文件的album_art。路徑是音頻文件路徑。

+0

謝謝,它的工作。 – saturngod 2012-12-04 08:37:37

+0

我已經在iOS 7中使用了上面的內容,但是在iOS 8中它現在會導致致命的崩潰。特別是[d objectForKey:@「data」]會導致無法識別的選擇器異常。 – 2014-09-27 12:41:28