因为这个 Spotify ios SDK v10beta 是新的并且有许多不完整的任务,所以要访问并能够播放播放列表中的所有曲目,您必须经过许多块。
首先我建议获取所有播放列表的第一个 SPTListPage 对象:
-(void)getFirstPlaylistPage
{
[SPTPlaylistList playlistsForUserWithSession:[SPTAuth defaultInstance].session callback:^(NSError *error, SPTListPage *playlistsPage) {
if (error != nil) {
NSLog(@"*** Getting playlists got error: %@", error);
return;
}
if (playlistsPage==nil) {
NSLog(@"*** No playlists were found for logged in user. ***");
return;
}
[self getFullPlaylistPage:playlistsPage];
}];
}
然后,将所有 SPTListPage 对象合并为一个:
-(void)getFullPlaylistPage:(SPTListPage*)listPage {
if (listPage.hasNextPage) {
[listPage requestNextPageWithSession:[SPTAuth defaultInstance].session callback:^(NSError *error, SPTListPage* playlistPage) {
if (error != nil) {
NSLog(@"*** Getting playlist page got error: %@", error);
return;
}
listPage = [listPage pageByAppendingPage:playlistPage];
[self getFullPlaylistPage:listPage];
}];
} else {
NSMutableArray* playlist = [[NSMutableArray alloc]init];
[self convertPlaylists:listPage arrayOfPlaylistSnapshots:playlist positionInListPage:0];
}
}
现在,SPTListPage 包含 SPTPartialPlaylist 对象,其中不包含播放列表的所有信息,因此我们需要将其转换为 SPTPlaylistSnapshot 对象:
-(void)convertPlaylists:(SPTListPage*)playlistPage arrayOfPlaylistSnapshots:(NSMutableArray*)playlist positionInListPage:(NSInteger)position
{
if (playlistPage.items.count > position) {
SPTPartialPlaylist* userPlaylist = playlistPage.items[position];
[SPTPlaylistSnapshot playlistWithURI:userPlaylist.uri session:[SPTAuth defaultInstance].session callback:^(NSError *error, SPTPlaylistSnapshot* playablePlaylist) {
if (error != nil) {
NSLog(@"*** Getting playlists got error: %@", error);
return;
}
if(!playablePlaylist){
NSLog(@"PlaylistSnapshot from call back is nil");
return;
}
[playlist addObject:playablePlaylist];
[self convertPlaylists:playlistPage arrayOfPlaylistSnapshots:playlist positionInListPage:position+1];
}];
} else {
// send your array of playlists somewhere example:
[self addSongs];
}
}
现在您可以通过简单的循环访问所有播放列表和其中的所有歌曲。
我希望 spotify 会在这里有所改进,因为它不是应该的样子。来吧,Spotify!