iOS - 音乐播放器之怎么获取音乐列表

时间:2022-06-17 01:44:17

方法一:

这个方法是通过获取到沙盒路径,来得到音乐的路径(使用这个方法需要把音乐放进沙盒)

NSFileManager *manager = [NSFileManager defaultManager];
NSString *Documents = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSLog(@"%@",Documents);
NSString *LocalStr = [[NSString alloc] initWithFormat:@"%@/Music",Documents];
NSLog(@"%@",LocalStr);
NSArray *LocalArr = [[NSArray alloc] init];
LocalArr = [manager contentsOfDirectoryAtPath:LocalStr error:nil];
for(int i = ;i < LocalArr.count;i++){
NSString *str1 = [[NSString alloc] init];
str1 = [LocalArr objectAtIndex:i];
MyMusic *music = [[MyMusic alloc] init];
music.path = [NSString stringWithFormat:@"%@/%@",LocalStr,str1];
NSLog(@"%@",music.path);
if([[str1 pathExtension] isEqualToString:@"mp3"]){
NSUInteger a = str1.length-;
NSRange range = {,a};
str1 = [str1 substringWithRange:range];
music.name = str1;
[self.musicArray addObject:music];
}
}

方法二:

通过访问ipod音乐库(此方法好像必须要真机调试的,因为模拟器上没有ipod音乐库)

MPMediaQuery *listQuery = [MPMediaQuery songsQuery];//playlistsQuery
NSArray *playlist = [listQuery collections];
MPMediaQuery *listQuery = [[MPMediaQuery alloc] init];
NSArray *playlist = [listQuery items];
for(MPMediaPlaylist *list in playlist){
NSArray *songs = [list items];
for(MPMediaItem *song in songs){
NSString *title = [song valueForProperty:MPMediaItemPropertyTitle];
NSString *artlist = [song valueForProperty:MPMediaItemPropertyArtist];
if(artlist != nil && ![self.musicArray containsObject:title]){
[self.musicArray addObject:title];
}
}
}

方法三:

扫描本地的音乐路径(这个方法直接把音乐放进xcode就好)

NSString *song = [[NSString alloc] init];
NSString *singer = [[NSString alloc] init];
UIImage *img;
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSLog(@"resourcePath = %@",resourcePath);
//取出资源目录下所有mp3文件
// NSArray *fileArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"mp3" inDirectory:resourcePath];
NSArray *mp3Array = [NSBundle pathsForResourcesOfType:@"mp3" inDirectory:[[NSBundle mainBundle] resourcePath]]; for(NSString *filePath in mp3Array){
NSLog(@"%@",filePath);
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
AVURLAsset *mp3Asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
for(NSString *format in [mp3Asset availableMetadataFormats]){
NSLog(@"%@",format);
for(AVMetadataItem *metadataItem in [mp3Asset metadataForFormat:format]){
NSLog(@"%@",metadataItem);
img = nil;
if([metadataItem.commonKey isEqualToString:@"title"]){
song = (NSString *)metadataItem.value;
}else if([metadataItem.commonKey isEqualToString:@"artist"]){
singer = (NSString *)metadataItem.value;
}
else if([metadataItem.commonKey isEqualToString:@"artwork"]){
NSData *data = (NSData*)(metadataItem.value);
img = [UIImage imageWithData:data];
} // else if([metadataItem.commonKey isEqualToString:@"albumName"]){
// albumName = (NSString *)metadataItem.value;
// }
// else if([metadataItem.commonKey isEqualToString:@"artwork"]){
// NSDictionary *dict = (NSDictionary *)metadataItem.value;
// NSData *data = [dict objectForKey:@"data"];
// <pre name="code" class="html">if([metadataItem.commonKey isEqualToString:@"artwork"]){
NSDictionary *dict = (NSDictionary *)metadataItem.value;
NSData *data = [dict objectForKey:@"data"];
image = [UIImage imageWithData:data];
self.albumImage.image = image; image = [UIImage imageWithData:data]; // self.albumImage.image = image; //} } MyMusic *music = [[MyMusic alloc] init]; music.name = song; music.singerName = singer; music.path = filePath; music.image = img; NSLog(@"%@,%@",music.name ,music.singerName); [self.musicArray addObject:music]; } }}

这个方法关于获取音乐的专辑图片我找晚上找到的方法都是下面这句

 if([metadataItem.commonKey isEqualToString:@"artwork"]){  

    NSDictionary *dict = (NSDictionary *)metadataItem.value;
NSData *data = [dict objectForKey:@"data"];//@"value"
image = [UIImage imageWithData:data];
self.albumImage.image = image;
}