?在開發(fā)音樂播放器時常常遇到要提取MP3文件的專輯信息和圖片的情況,那怎么提取呢?其實(shí)提取這些信息是非常簡單的。下面簡單的提取沙盒中的一個MP3文件來進(jìn)行說明:
? ? ?NSString *path =[ [NSBundle mainBundle] ?pathForResource:@"歌曲名" ofType:@"mp3"];
? ? ?NSURL ?*fileURL = [NSURL ?fileURLWithPath:path];
? ? ? AVURLAsset *avURLAsset = [[AVURLAsset alloc] initWithURL:fileURL options:nil];
? ? ? for (NSSting *format ?in [avURLAsset availableMetadataFormats]){
? ? ? ? ? ? ? for (AVMetadataItem *metadata in [avURLAsset metadataForFormat:format]){
? ? ? ? ? ? ? ? ? if([metadata.commonKey isEqualToString:@"title"]){
? ? ? ? ? ? ? ? ? ? ? ? NSSting *title = (NSSting *)metadata.value;//提取歌曲名
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? if([metadata.commonKey isEqualToString:@"artwork"]){
? ? ? ? ? ? ? ? ? ? ? ?UIImage *coverImage = [UIImage imageWithData:[(NSDictionary *)metadata.value objectForKey:@"data"]];//提取圖片
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? //還可以提取其他所需的信息
? ? ? ? ? ? ? ?}
? ? ? }
AVURLAsset 類的聲明在<AVFoundation/AVAsset.h>中,AVMetadataItem類的聲明在<AVFoundation/AVMetadataItem.h>中。至此,通過上面的代碼你可以得到所需要的MP3中的信息。