由於此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!
如何返回播放列表中的所有歌曲? –