如何在ios开发中从mp3文件中提取元数据 [英] How can I extract metadata from mp3 file in ios development

查看:179
本文介绍了如何在ios开发中从mp3文件中提取元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on an ios music player with cloud storage.

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

I need to extract the music information such as title, artist, artwork.

我有一个名为playit的动作,可以播放和暂停mp3文件。它还应该使用与mp3文件关联的metadtaa填充一些UILables和UIImage。问题是我无法从不同的25个mp3文件中提取元数据。这是我的代码:

I have an action called playit which plays and pauses the mp3 file. It should also populate some UILables and UIImage with the metadtaa that is associated with the mp3 file. The problem is that I could not get the metadata extracted from more than different 25 mp3 files. Here is my code:

文件网址是正确的,因为音频播放器能够找到并播放它,但我不知道为什么avmetadataitem无法获取元数据。

The file url is correct because the audio player is able to find and play it, but I do not know why avmetadataitem is not able to get the metadata.

- (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) {

这篇关于如何在ios开发中从mp3文件中提取元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆