我怎样才能提取iOS开发从MP3文件的元数据(How can I extract metadata

2019-08-01 11:20发布

我工作在iOS音乐播放器,云存储。

我需要提取的音乐信息,例如标题,艺术家,艺术作品。

我有一个动作叫playit其播放和暂停的MP3文件。 还应该填充一些UILables和UIImage的与与MP3文件关联的metadtaa。 问题是,我无法得到超过25不同的MP3文件中提取元数据。 这里是我的代码:

文件的URL是正确的,因为音频播放器能够找到并播放,但我不知道为什么avmetadataitem是不是能够得到的元数据。

- (IBAction)playIt:(id)sender {
AVAudioPlayer *audioPlayer;
AVAsset *assest;

NSString * applicationPath = [[NSBundle mainBundle] resourcePath]; 
NSString *secondParentPath = [applicationPath stringByDeletingLastPathComponent];
NSString *soundFilePath = [[secondParentPath stringByAppendingPathComponent:@"fisal1407"] stringByAppendingPathComponent:[musicFiles objectForKey:@"show_id"] ];

NSURL *fileURL = [NSURL URLWithString:[soundFilePath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];

assest = [AVURLAsset URLAssetWithURL:fileURL options:nil];
NSArray *metadata = [assest commonMetadata];
for (NSString *format in metadata) {
    for (AVMetadataItem *item in [assest metadataForFormat:format]) {
        if ([[item commonKey] isEqualToString:@"title"]) {
            filename.text = (NSString *)[item value];
            NSLog(@" title : %@", (NSString *)[item value]);
        } 
        if ([[item commonKey] isEqualToString:@"artist"]) {
            show_id.text = (NSString *)[item value];
        }
        if ([[item commonKey] isEqualToString:@"albumName"]) {
          //  _albumName = (NSString *)[item value];
        }
        if ([[item commonKey] isEqualToString:@"artwork"]) {
            NSData *data = [(NSDictionary *)[item value] objectForKey:@"data"];
            UIImage *img = [UIImage imageWithData:data]  ;
            imageView.image = img;
            continue;
         }
    }
 }

   if (audioPlayer == nil) {

    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
    audioPlayer.numberOfLoops = -1;

   [audioPlayer play];
    [sender setImage:[UIImage imageNamed:@"player_044.gif"] forState:UIControlStateNormal];
   }
else
   {
    if (audioPlayer.isPlaying)
    {
        [sender setImage:[UIImage imageNamed:@"player_04.gif"] forState:UIControlStateNormal];
        [audioPlayer pause];
    } else {
        [sender setImage:[UIImage imageNamed:@"player_044.gif"] forState:UIControlStateNormal];
        [audioPlayer play];
    }
 }  
}

Answer 1:

尝试

for (NSString *format in [asset availableMetadataFormats])

代替

NSArray *metadata = [assest commonMetadata];
for (NSString *format in metadata) {


文章来源: How can I extract metadata from mp3 file in ios development