16

我的代码:

- (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则为空。我做错了什么,我该如何解决?

4

3 回答 3

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;
}
于 2012-12-27T11:50:38.890 回答
11

我刚刚在这里找到了答案:如何在 ios 开发中从 mp3 文件中提取元数据,现在正在寻找为什么该代码不适用于 m4a 文件。我从您的代码中提取了一些内容,并提供了以下似乎适用于 m4a 和 mp3 的内容。请注意,我将适用于 mp3 的代码保留为没有限定符的开放 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;
        }
     }
  }
}
于 2012-12-26T21:29:35.090 回答
0

正如 Ryan Heitner 在对 Andy Weinstein 的评论中指出的那样,AVMetadataKeySpaceID3从 IOS7 到 IOS8 检索二进制艺术品数据的方式已经发生了变化。在ID3的情况下,一味地铸造itemNSDictionary崩溃。要使代码防弹,只需检查 dataValue,否则检查 Classes。事实上今天是item.valueitem.dataValueNSData课。

- (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];
}
于 2020-01-18T13:30:41.043 回答