获取MP3的信息

方法1:

 1 //读取mp3里的信息
 2     AudioFileID fileID;
 3     
 4     AudioFileTypeID typeIdMP3 = kAudioFileMP3Type;
 5     
 6     OSStatus status = noErr;
 7     //读取mp3文件
 8     status = AudioFileOpenURL((__bridge CFURLRef)fileURL, kAudioFileReadPermission, typeIdMP3, &fileID);
 9     if (status != noErr) {
10         NSLog(@"AudioFileOpenURL Failure");
11     }
12     
13 //    ID3,一般是位于一个mp3文件的开头或末尾的若干字节内,附加了关于该mp3的歌手,标题,专辑名称,年代,风格等信息,该信息就被称为ID3信息,ID3信息分为两个版本,v1和v2版。 其中:v1版的ID3在mp3文件的末尾128字节,以TAG三个字符开头,后面跟上歌曲信息。 v2版一般位于mp3的开头,可以存储歌词,该专辑的图片等大容量的信息。
14     UInt32 id3DataSize = 0;
15     status = AudioFileGetPropertyInfo(fileID, kAudioFilePropertyID3Tag, &id3DataSize, NULL);
16     if (status != noErr) {
17         NSLog(@"读id3DataSize失败,没有附加信息");
18     }
19     NSLog(@"id3DataSize: %ld",id3DataSize);
20     
21     //唱片信息
22     NSDictionary *propertyInfo = nil;
23     UInt32 piDataSize = sizeof(propertyInfo);
24     status = AudioFileGetProperty(fileID, kAudioFilePropertyInfoDictionary, &piDataSize, &propertyInfo);
25     if (status != noErr) {
26         NSLog(@"读取唱片信息失败");
27     }
28     NSLog(@"propertyInfo: %@",propertyInfo);
29     
30     
31     
32     //唱片封面 这里获取到的图片数据是空的,暂时不知道为什么,建议使用方法2,简单
33     CFDataRef albumPic = nil;
34     UInt32 albumPicDataSize = sizeof(albumPic);
35     status = AudioFileGetProperty(fileID, kAudioFilePropertyAlbumArtwork, &albumPicDataSize, &albumPic);
36     
37     if (status != noErr) {
38         NSLog(@"读取唱片封面失败");
39     }
40     //获取专辑名称
41     NSString *album = propertyInfo[@"album"];
42     NSString *artist = propertyInfo[@"artist"];
43     NSString *title = propertyInfo[@"title"];
44     NSString *year = propertyInfo[@"year"];
45     CZMP3Info *mp3Info = [[CZMP3Info alloc] init];
46 
47     mp3Info.album = album;
48     mp3Info.artist = artist;
49     mp3Info.title = title;
50     mp3Info.year = year;
51     mp3Info.albumArtwork = (__bridge NSData *)albumPic;

 

方法2:

 1 AVURLAsset *assest = [AVURLAsset URLAssetWithURL:fileURL options:nil];
 2     
 3     CZMP3Info *mp3Info = [[CZMP3Info alloc] init];
 4     NSLog(@"%@",assest.lyrics);
 5     for (NSString *key in [assest availableMetadataFormats]) {
 6         for (AVMetadataItem *item in [assest metadataForFormat:key]) {
 7             NSString *commonKey = item.commonKey;
 8             
 9             if ([commonKey isEqualToString:@"artist"]) {//歌手
10                 mp3Info.artist = (NSString *)item.value;
11             }else if([commonKey isEqualToString:@"albumName"]){//专辑名称
12                 mp3Info.album = (NSString *)item.value;
13             }else if([commonKey isEqualToString:@"title"]){//歌曲名
14                 mp3Info.title = (NSString *)item.value;
15             }else if ([commonKey isEqualToString:@"artwork"]){
16                 NSDictionary *artworkDict = (NSDictionary *)item.value;
17                 NSData *image = [artworkDict objectForKey:@"data"];
18                 mp3Info.albumArtwork = image;
19             }
20         }
21     }
22     
23     CMTime durationTime = assest.duration;
24     CGFloat duration = CMTimeGetSeconds(durationTime);

 

posted @ 2014-07-29 08:01  Vincent_Guo  阅读(2315)  评论(0编辑  收藏  举报