IOS菜鸟的所感所思(十四)——解析专辑获得专辑中的歌曲以及图片的优化

目标:解析专辑获得专辑中的歌曲以及图片的优化

步聚: 1.布局联线,在storyboard中添加一个UICollectionViewController中视图控制器。
2.解析专辑接口,接口的参数如下:

获取专辑所包含的歌曲的接口:
请求地址: http://music.163.com/api/album/albumid  //albumid为当前欲请求的专辑id


需要设置的HTTP Haeder:
    "Accept-Encoding”: "deflate,gzip"  


    "Referer”:   "http://music.163.com/"


    "User-Agent”:  "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"


需要设置的HTTP Method:
    GET



3.代码实现:
a.接口的实现

FetchDataFromNet.h中:


typedef void (^fetchMusicInAlbum)(NSArray *musicArray, NSError *error);


FetchDataFromNet.m中:

+ (void)fetchAlbumMusic:(id)item callback:(fetchMusicInAlbum)callback{

//albumID在上一个接口中已经获取得到了

    NSURL *songURLInAlbum = [NSURL URLWithString:[NSString stringWithFormat:@"http://music.163.com/api/album/%@",[item valueForKey:@"albumID"]]];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:songURLInAlbum];

    

    [request setValue:@"deflate,gzip" forHTTPHeaderField:@"Accept-Encoding"];

    [request setValue:@"http://music.163.com/" forHTTPHeaderField:@"Referer"];

    [request setValue:@"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)" forHTTPHeaderField:@"User-Agent"];

    [request setHTTPMethod:@"GET"];

    

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        if (connectionError) {

            callback(nil,connectionError);

        }else{

            NSMutableArray *musicArray = [NSMutableArray new];

            @try {

                NSDictionary *musicDic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

//                NSLog(@"music in Album:%@",musicDic);

                NSArray *musicInAlbum = [[musicDic objectForKey:@"album"]objectForKey:@"songs"];

//                NSLog(@"songs :%@",musicInAlbum[2]);

                NSLog(@"count:%ld",[musicInAlbum count]);

                [musicInAlbum enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

                    SongsInAlbum *song = [SongsInAlbum songsInAlbumWithDic:obj];

                    if (song) {

                        [musicArray addObject:song];

                    }

                }];

            }

            @catch (NSException *exception) {

                

            }

            @finally {

                callback(musicArray,nil);

            }

        }

    }];

}

b.在相应的视图控制器中获取数据源。

- (void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];

    

    [self fetchMusicInAlbum];

}

- (void)fetchMusicInAlbum{

    [FetchDataFromNet fetchAlbumMusic:self.albumData callback:^(NSArray *musicArray, NSError *error) {

        if(error){

            NSLog(@"%@",error);

        }else{

            self.songsArrayInAlbum = musicArray;

            NSLog(@"%@",self.songsArrayInAlbum);

            [self.collectionView reloadData];

        }

    }];

}

c.在视图加载时需要的准备。

- (void)viewDidLoad {

    [super viewDidLoad];

//该视图控制器中的一个属性AlbumData *albumData;


    self.title = self.albumData.albumName;

    self.collectionView.delegate = self;

    self.collectionView.dataSource = self;

    

    UINib *cellNib = [UINib nibWithNibName:@"collectionCell" bundle:[NSBundle mainBundle]];

    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:albumReuseIdentifier];

    

    

}


d.加载数据。

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;

}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return [self.songsArrayInAlbum count];

}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    

    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:albumReuseIdentifier forIndexPath:indexPath];

    

    [cell setSongInAlbum:_songsArrayInAlbum[indexPath.row]];

    

    return cell;

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

    return CGSizeMake(self.view.frame.size.width,50);

}


这样就可以获得专辑中的歌曲了。

至于图片的圆角显示:
在我们自定义 CollectViewCell,CollectionViewCell中的初始化cell的信息中

- (void)setAlbumInfo:(AlbumData *)albumData,

- (void)setInfo:(Music *)music等方法中添加[self imageProperty];

而该方法的具体实现:

- (void)imageProperty{

    self.imageLogo.layer.borderColor = [UIColor grayColor].CGColor;

    self.imageLogo.layer.borderWidth = 2.0f;

    self.imageLogo.layer.cornerRadius = 8;

    

    self.imageLogo.layer.masksToBounds = YES;

}

效果:



说明:如果你到现在这些功能都实现了,其余的你可以自己添枝加叶,渐渐完善这个项目。你可以试着实现以下的功能:
播放器里面需要实现的一系列基础功能:
1.播放/暂停
2.歌曲切换:上一曲/下一曲
3.播放模式:循环播放/单曲播放/随机播放/顺序播放......
4.音量控制
5.播放进度控制:更新播放进度条/拖动进度条在指定时间处播放/播放时长/缓冲进度
6.歌曲基本信息展示:歌曲名/艺术家
7.歌词


这需要另一个视图控制器来实现。我的这个项目到这儿就结束了。












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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值