Ios 从播放列表Spotify获取所有曲目
我可以使用spotify ios sdk获取所有用户的播放列表,这是我的代码Ios 从播放列表Spotify获取所有曲目,ios,spotify,Ios,Spotify,我可以使用spotify ios sdk获取所有用户的播放列表,这是我的代码 [SPTPlaylistList playlistsForUserWithSession:session callback:^(NSError *error, id object) { SPTListPage *pl= object; NSLog(@"%@",pl.items); }]; 但我现在不知道如何获得这些播放列表的所有曲目。有什么帮助吗?因为这款Spotify ios SDK v10beta
[SPTPlaylistList playlistsForUserWithSession:session callback:^(NSError *error, id object) {
SPTListPage *pl= object;
NSLog(@"%@",pl.items);
}];
但我现在不知道如何获得这些播放列表的所有曲目。有什么帮助吗?因为这款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 使用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中找到一个SpotifyAPI的弃用版本的示例,我在向下滚动时看到了“Using Swift3:”。。。然后试试看。。。它是有效的。非常感谢。