Reputation: 63
I was able to do this with CocoaLibSpotify, but I can't figure out how it should be done in the new Spotify iOS SDK.
I'm trying to create an array of tracks from multiple playlists that I load in via their URIs. The goal here is to be able to play, at random, tracks from across a set of playlists.
I've been able to load in a playlist from its URI, it's represented as an SPPlaylistSnapshot. There doesn't seem to be a way to get the individual tracks from this SPPlaylistSnapshot so I can create a pool that I can add to and draw from.
Does anyone know a way to do this?
Upvotes: 1
Views: 369
Reputation: 46
Basically what everyone's saying is correct, use SPTPlaylistSnapshot to get the tracks for the playlist, here's my code for getting all the items in a playlist in Swift 3.
func getTracksFrom(page:SPTListPage, allItems:[SPTPlaylistTrack]) -> [SPTPlaylistTrack] {
guard let items = page.items as? [SPTPlaylistTrack] else {print("empty page");return allItems}
var allTracks = allItems
allTracks.append(contentsOf: items)
var nextPage = SPTListPage()
if page.hasNextPage {
page.requestNextPage(withAccessToken: (SPTAuth.defaultInstance().session.accessToken)!) { (error, data) in
guard let p = data as? SPTListPage else {return}
nextPage = p
}
return getTracksFrom(page:nextPage,allItems:allTracks)
}
return allTracks
}
func getAllTracksFrom(_ playlist:SPTPlaylistSnapshot) -> [SPTPlaylistTrack] {
var allTracks:[SPTPlaylistTrack] = []
allTracks = getTracksFrom(page: playlist.firstTrackPage, allItems: allTracks)
return allTracks
}
override func viewDidLoad() {
super.viewDidLoad()
guard let p = playlist else {return}
SPTPlaylistSnapshot.playlist(withURI: p.uri, accessToken: (SPTAuth.defaultInstance().session.accessToken)!, callback: { (error, dataOrNil) in
guard error == nil else {print(error!);return}
guard let playlistSnapshot = dataOrNil as? SPTPlaylistSnapshot else {print("couldn't cast as SPTPlaylistSnapshot");return}
self.tracks = self.getAllTracksFrom(playlistSnapshot)
self.tableView.reloadData()
})
}
Upvotes: 0
Reputation: 31
After authentication, define a playlist request , like this:
let playListRequest = try! SPTPlaylistList.createRequestForGettingPlaylists(forUser: userName, withAccessToken: token)
I use alamofire to post this request:
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)
}
}
}
}
}
}
}
Upvotes: 0
Reputation: 18776
SPTPlaylistSnapshot
has firstTrackPage
property, which contains the first x tracks, where x is a number I can't remember. With that first page, you can request additional pages until you have all the tracks.
See the documentation for SPTPlaylistSnapshot
and SPTListPage
for details.
Upvotes: 1