ios开发---音乐播放器之怎么获取音乐列表

博主刚学ios开发不久,最近想要自己写一个音乐播放器。结果昨天关于怎么获取音乐列表的问题在网上找了好久,今天想要把我昨天找到的资料和学到的东西记录下来。

方法一:

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

    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 = 0;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-4;
            NSRange range = {0,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]; } }}

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

<pre name="code" class="objc">if([metadataItem.commonKey isEqualToString:@"artwork"]){
                                NSDictionary *dict = (NSDictionary *)metadataItem.value;
                                NSData *data = [dict objectForKey:@"data"];//@"value"
                                image = [UIImage imageWithData:data];
                                self.albumImage.image = image;


 
结果一运行到NSData *data = [dict objectForKey:@"data"];就崩溃 我在网上找了好久都不知道怎么解决,后来贴吧的一位大神帮我解决了,就是我上面的那段代码。 

我把昨天遇到的问题和学到的东西记录下来希望能对以后遇到相同问题的朋友有所帮助(这是博主第一次的原创博客,写的有不对的请多多指教)

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值