今天在做一款音樂播放器的時候需要支持文件共享本地導(dǎo)入音樂,但是導(dǎo)入進(jìn)去的mp3是只有歌曲名字加格式的济赎,有些甚至歌曲名字都不對只是一個.mp3文件,例如:
但是這時候我們的界面上是需要顯示歌曲的信息的草添,歌手酪劫,歌曲名字,專輯珊蟀,縮略圖等等信息菊值,其實(shí)這些信息在.mp3里面是可以找到的,但是也不能全找到育灸。估計是這些生成的時候沒有加到里面腻窒,話不多說直接進(jìn)入正題。這里以我的用法為例磅崭,可根據(jù)自己情況靈活變動儿子。
獲取到mp3文件(我這里是獲取的所有文件放到數(shù)組。如不需要可以單個獲仍矣鳌)
NSArray *mp3Array = [NSBundle pathsForResourcesOfType:@"mp3" inDirectory:[[NSBundle mainBundle] resourcePath]];遍歷數(shù)組得到每個文件的路徑并得到AVURLAsset的對象
for (NSString *filePath in mp3Array) {
NSURL *url = [NSURL fileURLWithPath:filePath];
NSString *MusicName = [filePath lastPathComponent];
AVURLAsset *mp3Asset = [AVURLAsset URLAssetWithURL:url options:nil];
NSLog(@"%@",mp3Asset);獲取文件中數(shù)據(jù)格式類型
for (NSString *format in [mp3Asset availableMetadataFormats]) {-
獲取特定格式類型
for (AVMetadataItem *metadataItem in [mp3Asset metadataForFormat:format]) {
NSLog(@"commonKey = %@",metadataItem.commonKey);if ([metadataItem.commonKey isEqualToString:@"artwork"]) { NSString *mime = [(NSDictionary *)metadataItem.value objectForKey:@"MIME"]; NSData *data = [(NSDictionary *)metadataItem.value objectForKey:@"data"]; UIImageView.image = [UIImage imageWithData:data]; NSLog(@"mime: %@",mime); } else if([metadataItem.commonKey isEqualToString:@"title"]) { NSString *title = (NSString *)metadataItem.value; NSLog(@"title: %@",title); } else if([metadataItem.commonKey isEqualToString:@"artist"]) { NSString *artist = (NSString *)metadataItem.value; NSLog(@"artist: %@",artist); } else if([metadataItem.commonKey isEqualToString:@"albumName"]) { NSString *albumName = (NSString *)metadataItem.value; NSLog(@"albumName: %@",albumName); }
以上就是具體如何獲取本地的歌曲信息了柔逼。如有不明白可參考這篇內(nèi)容:
-
下面為大家貼上本人在項目中的實(shí)例應(yīng)用代碼:
+ (NSMutableArray *)MusicInfoArray { NSMutableArray *resultArray = [[NSMutableArray alloc] init]; NSArray *mp3Array = [NSBundle pathsForResourcesOfType:@"mp3" inDirectory:[[NSBundle mainBundle] resourcePath]]; for (NSString *filePath in mp3Array) { NSURL *url = [NSURL fileURLWithPath:filePath]; NSString *MusicName = [filePath lastPathComponent]; AVURLAsset *mp3Asset = [AVURLAsset URLAssetWithURL:url options:nil]; NSLog(@"%@",mp3Asset); for (NSString *format in [mp3Asset availableMetadataFormats]) { NSMutableDictionary *infoDict = [[NSMutableDictionary alloc] init]; [infoDict setObject:MusicName forKey:@"MusicName"]; NSLog(@"format type = %@",format); for (AVMetadataItem *metadataItem in [mp3Asset metadataForFormat:format]) { NSLog(@"commonKey = %@",metadataItem.commonKey); if ([metadataItem.commonKey isEqualToString:@"artwork"]) { NSString *mime = [(NSDictionary *)metadataItem.value objectForKey:@"MIME"]; NSLog(@"mime: %@",mime); [infoDict setObject:mime forKey:@"artwork"]; } else if([metadataItem.commonKey isEqualToString:@"title"]) { NSString *title = (NSString *)metadataItem.value; NSLog(@"title: %@",title); [infoDict setObject:title forKey:@"title"]; } else if([metadataItem.commonKey isEqualToString:@"artist"]) { NSString *artist = (NSString *)metadataItem.value; NSLog(@"artist: %@",artist); [infoDict setObject:artist forKey:@"artist"]; } else if([metadataItem.commonKey isEqualToString:@"albumName"]) { NSString *albumName = (NSString *)metadataItem.value; NSLog(@"albumName: %@",albumName); [infoDict setObject:albumName forKey:@"albumName"]; } } [resultArray addObject:infoDict]; } } return resultArray; }
每一次的學(xué)習(xí)和記錄都是成長道路上的一小步。