如何在ios开发中从mp3文件中提取元数据

我正在使用云存储的ios音乐播放器。

我需要提取标题,艺术家,艺术品等音乐信息。

我有一个叫做playit的动作,可以播放和暂停mp3文件。 它还应该使用与mp3文件关联的metadtaa来填充一些UILables和UIImage。 问题是我无法从超过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];
    }
 }  
}

尝试

for (NSString *format in [asset availableMetadataFormats])

代替

NSArray *metadata = [assest commonMetadata];
for (NSString *format in metadata) {
链接地址: http://www.djcxy.com/p/11313.html

上一篇: How can I extract metadata from mp3 file in ios development

下一篇: How to properly structure a KnockoutJS application