【问题标题】:get all the tracks from playlist Spotify从播放列表 Spotify 中获取所有曲目
【发布时间】:2015-09-08 02:24:04
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios spotify


    【解决方案1】:

    因为这个 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!

    【讨论】:

    • 如何返回播放列表的所有歌曲?
    【解决方案2】:

    使用 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)
    
                                }
                            }
                        }
    
    
                    }
                }
            }
    }
    

    【讨论】:

    • 我想拥抱你。搜索示例以希望在objective-c 中找到一个已弃用的spotify api 版本,我在向下滚动时看到“使用Swift3:”......然后尝试它......它有效。谢谢。
    猜你喜欢
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多