目标:解析专辑获得专辑中的歌曲以及图片的优化
请求地址: 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
FetchDataFromNet.h中:
typedef void (^fetchMusicInAlbum)(NSArray *musicArray, NSError *error);
+ (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];
}
- (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);
}
- (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.歌词
本文详细阐述了如何解析专辑接口以获取专辑内的歌曲及图片,并通过代码实现展示了具体步骤。包括请求地址、HTTP头设置、接口调用、数据解析以及视图控制器中的数据加载与显示,最终实现专辑图片的圆角显示。
2807

被折叠的 条评论
为什么被折叠?



