0

i want to get the album picture in mp3 file,and here is part of my code

    //the Chinese is music name 
    NSURL *fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"安和桥" ofType:@"mp3"]];
    AudioFileTypeID fileTypeHint = kAudioFileMP3Type;

    AudioFileID fileID = nil;
    OSStatus err = noErr;

    err = AudioFileOpenURL((__bridge CFURLRef)fileURL, kAudioFileReadPermission, 0, &fileID);

    UInt32 id3DataSize = 0;
    err = AudioFileGetPropertyInfo(fileID, kAudioFilePropertyID3Tag, &id3DataSize, NULL);
    NSDictionary *piDict = nil;
    UInt32 piDataSize = sizeof(piDict);
    err = AudioFileGetProperty(fileID, kAudioFilePropertyInfoDictionary, &piDataSize, &piDict);
    CFDataRef AlbumPic = nil;
    UInt32 picDataSize = sizeof(picDataSize);
    err = AudioFileGetProperty(fileID, kAudioFilePropertyAlbumArtwork, &picDataSize, &AlbumPic);
    NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
    NSLog(@"Error: %@", [error description]);
    NSData *imgdata = (__bridge NSData *)AlbumPic

i can get the album name correctly , but image data is nil.and i print the error information

2014-09-09 20:07:05.129 Music_Player[2792:60b] Error: Error Domain=NSOSStatusErrorDomain Code=1886681407 "The operation couldn’t be completed. (OSStatus error 1886681407.)"

in another question ,someone says it may be the permisssion problem , but it's set property , however,i am getting property,so i think it can't be that reason.but i really don't know why? AudioFileSetProperty returning 'kAudioFileUnsupportedPropertyError (pty?)'

Community
  • 1
  • 1
Wythe
  • 149
  • 3
  • 12

1 Answers1

0

Sometimes you can not get all the information from audio file using AudioFile. Use AVURLAsset instead.

- (void)getAlbumArtworkInfo
{
   NSURL *fileUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"abc" ofType:@"mp3"]];
   AVURLAsset *avURLAsset = [AVURLAsset URLAssetWithURL:fileUrl options:nil];
   for (NSString *format in [avURLAsset availableMetadataFormats]) {
      for (AVMetadataItem *metadataItem in [avURLAsset metadataForFormat:format]) {
          if ([metadataItem.commonKey isEqualToString:@"artwork"]) {
              UIImage *artImageInMp3 = [UIImage imageWithData:[(NSDictionary*)metadataItem.value objectForKey:@"data"]];
              NSLog(@"artImageInMp3 %@",artImageInMp3);
              break;
          }
      }
   }
}
gabbler
  • 13,626
  • 4
  • 32
  • 44
  • it works as what you said,but i still don't get very well,usually we use this method AudioFile to get what information? – Wythe Sep 10 '14 at 11:34
  • Using AudioFile you usually get information such as title, artist, duration and album. – gabbler Sep 10 '14 at 14:40
  • and use AVURLAsset ,i can get all these information? – Wythe Sep 11 '14 at 01:07
  • 1
    Sometimes using AVURLAsset you can't get ID3 information, you can look at this info [two methods](http://homerzuo.blogspot.com/2013/04/the-way-to-extract-id3-information-from.html) – gabbler Sep 11 '14 at 01:16