iOS AVFoundation:如何从mp3文件中获取图稿?

时间:2012-12-25 12:43:18

标签: objective-c ios avfoundation

我的代码:

- (void)metadata {
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:self.fileURL options:nil];

NSArray *artworks = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyArtwork keySpace:AVMetadataKeySpaceCommon];
NSArray *titles = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyTitle keySpace:AVMetadataKeySpaceCommon];
NSArray *artists = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyArtist keySpace:AVMetadataKeySpaceCommon];
NSArray *albumNames = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyAlbumName keySpace:AVMetadataKeySpaceCommon];

AVMetadataItem *artwork = [artworks objectAtIndex:0];
AVMetadataItem *title = [titles objectAtIndex:0];
AVMetadataItem *artist = [artists objectAtIndex:0];
AVMetadataItem *albumName = [albumNames objectAtIndex:0];

if ([artwork.keySpace isEqualToString:AVMetadataKeySpaceID3]) {
    NSDictionary *dictionary = [artwork.value copyWithZone:nil];
    self.currentSongArtwork = [UIImage imageWithData:[dictionary objectForKey:@"data"]];
}
else if ([artwork.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
    self.currentSongArtwork = [UIImage imageWithData:[artwork.value copyWithZone:nil]];
}

self.currentSongTitle = [title.value copyWithZone:nil];
self.currentSongArtist = [artist.value copyWithZone:nil];
self.currentSongAlbumName = [albumName.value copyWithZone:nil];
self.currentSongDuration = self.audioPlayer.duration;
}

这适用于从m4a文件中获取图稿,但不适用于mp3文件。如果asset指向mp3文件,则artworks为空。我做错了什么,如何解决?

3 个答案:

答案 0 :(得分:16)

我发现在同步设置图像时,艺术品是异步加载的。我这样解决了:

- (void)metadata {
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:self.fileURL options:nil];

NSArray *titles = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyTitle keySpace:AVMetadataKeySpaceCommon];
NSArray *artists = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyArtist keySpace:AVMetadataKeySpaceCommon];
NSArray *albumNames = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyAlbumName keySpace:AVMetadataKeySpaceCommon];

AVMetadataItem *title = [titles objectAtIndex:0];
AVMetadataItem *artist = [artists objectAtIndex:0];
AVMetadataItem *albumName = [albumNames objectAtIndex:0];


NSArray *keys = [NSArray arrayWithObjects:@"commonMetadata", nil];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
    NSArray *artworks = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata
                                                       withKey:AVMetadataCommonKeyArtwork
                                                      keySpace:AVMetadataKeySpaceCommon];

    for (AVMetadataItem *item in artworks) {
        if ([item.keySpace isEqualToString:AVMetadataKeySpaceID3]) {
            NSDictionary *d = [item.value copyWithZone:nil];
            self.currentSongArtwork = [UIImage imageWithData:[d objectForKey:@"data"]];
        } else if ([item.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
            self.currentSongArtwork = [UIImage imageWithData:[item.value copyWithZone:nil]];
        }
    }
}];


self.currentSongTitle = [title.value copyWithZone:nil];
self.currentSongArtist = [artist.value copyWithZone:nil];
self.currentSongAlbumName = [albumName.value copyWithZone:nil];
self.currentSongDuration = self.audioPlayer.duration;
}

答案 1 :(得分:11)

我刚刚在这里找到了答案:How can I extract metadata from mp3 file in ios development现在正在寻找为什么该代码不适用于m4a文件。 我从你的代码中拿了一点,并提出以下似乎适用于m4a和mp3的内容。请注意,我将用于mp3的代码保留为没有限定符的open else子句 - 如果有人获得更多经验,欢迎他们改进!

 - (void)getMetaDataForSong:(MusicItem *)musicItem {
   AVAsset *assest;

   // filePath looks something like this: /var/mobile/Applications/741647B1-1341-4203-8CFA-9D0C555D670A/Library/Caches/All Summer Long.m4a

    NSURL *fileURL = [NSURL fileURLWithPath:musicItem.filePath];
    NSLog(@"%@", fileURL);
    assest = [AVURLAsset URLAssetWithURL:fileURL    options:nil];
    NSLog(@"%@", assest);

    for (NSString *format in [assest availableMetadataFormats]) {
      for (AVMetadataItem *item in [assest metadataForFormat:format]) {
        if ([[item commonKey] isEqualToString:@"title"]) {
            musicItem.strSongTitle = (NSString *)[item value];
        } 
        if ([[item commonKey] isEqualToString:@"artist"]) {
            musicItem.strArtistName = (NSString *)[item value];
        }
        if ([[item commonKey] isEqualToString:@"albumName"]) {
          musicItem.strAlbumName = (NSString *)[item value];
        }
        if ([[item commonKey] isEqualToString:@"artwork"]) {
          UIImage *img = nil;
          if ([item.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
            img = [UIImage imageWithData:[item.value copyWithZone:nil]];
          }
          else { // if ([item.keySpace isEqualToString:AVMetadataKeySpaceID3]) {
            NSData *data = [(NSDictionary *)[item value] objectForKey:@"data"];
            img = [UIImage imageWithData:data]  ;
          }
          musicItem.imgArtwork = img;
        }
     }
  }
}

答案 2 :(得分:0)

正如Ryan Heitner在对Andy Weinstein的评论中所指出的那样,从AVMetadataKeySpaceID3检索二进制图稿数据的方式已从IOS7更改为IOS8。如今,在ID3案例中,将item盲目地投射到NSDictionary会崩溃。要使代码防弹,只需检查dataValue,否则检查Classs。实际上,今天item.value指向item.dataValue并具有NSData类。

- (UIImage*)imageFromItem:(AVMetadataItem*)item
{
  NSData* data=nil;
  if (item.dataValue!=nil) {
    data=item.dataValue;
  } else if ([item.value isKindOfClass:NSData.class]) { //never arrive here nowadays
    data=(NSData*)item.value;
  } else if ([item.value isKindOfClass:NSDictionary.class]) { //never arrive here nowadays...
    NSDictionary* dict=(NSDictionary*)item.value;
    data=dict[@"data"];
  }
  if (data==nil) {
    return nil;
  }
  return [UIImage imageWithData:data];
}