2015-06-22 107 views
2

我能夠使用spotify ios sdk獲取用戶的所有播放列表,這是我的代碼。獲取播放列表中的所有曲目Spotify

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

但我現在還不確定如何獲得這些播放列表的所有曲目。任何幫助?

回答

3

由於此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!

+0

如何返回播放列表中的所有歌曲? –

1

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

          } 
         } 
        } 


       } 
      } 
     } 
} 
+0

我想抱抱你。搜索一個希望在objective-c中找到一個用於spotify api的已棄用版本的示例,我在向下滾動時看到「使用Swift3:」然後嘗試它......並且它可以正常工作。謝謝。 – pachun

相關問題