从播放列表Spotify中获取所有曲目

时间:2015-06-22 14:58:21

标签: ios spotify

我能够使用spotify ios sdk获取所有用户的播放列表,这是我的代码。

[SPTPlaylistList playlistsForUserWithSession:session callback:^(NSError *error, id object) {
    SPTListPage *pl= object;
    NSLog(@"%@",pl.items);
}];

但我现在不确定如何获取这些播放列表的所有曲目。有什么帮助吗?

2 个答案:

答案 0 :(得分:4)

由于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!

答案 1 :(得分:3)

使用Swift3:

验证后,定义播放列表请求,如下所示:

let playListRequest = try! SPTPlaylistList.createRequestForGettingPlaylists(forUser: userName, withAccessToken: token)

我使用alamofire发布此请求:

Alamofire.request(playListRequest)
        .response { response in


        let list = try! SPTPlaylistList(from: response.data, with: response.response)

        for playList in list.items  {
            if let playlist = playList as? SPTPartialPlaylist {
                print( playlist.name ) // playlist name
                print( playlist.uri)    // playlist uri

                let stringFromUrl =  playlist.uri.absoluteString
                let uri = URL(string: stringFromUrl)
                // use SPTPlaylistSnapshot to get all the playlists
                SPTPlaylistSnapshot.playlist(withURI: uri, accessToken: token!) { (error, snap) in
                    if let s = snap as? SPTPlaylistSnapshot {
                        // get the tracks for each playlist
                        print(s.name) 

                        for track in s.firstTrackPage.items {
                            if let thistrack = track as? SPTPlaylistTrack {

                                print(thistrack.name)

                            }
                        }
                    }


                }
            }
        }
}