獲取MP3的信息

方法1:spa

 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:code

 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);
相關文章
相關標籤/搜索