[英]Creating an array of tracks from multiple playlists in Spotify iOS SDK 3.0
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. 我可以使用CocoaLibSpotify做到这一点,但是我不知道应该如何在新的Spotify iOS SDK中完成它。
I'm trying to create an array of tracks from multiple playlists that I load in via their URIs. 我正在尝试通过我通过其URI加载的多个播放列表创建曲目阵列。 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. 我已经能够从其URI加载播放列表,它表示为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.
似乎没有办法从此SPPlaylistSnapshot中获取单个曲目,因此我可以创建一个可以添加到其中或从中绘制的池。
Does anyone know a way to do this? 有人知道这样做的方法吗?
SPTPlaylistSnapshot
has firstTrackPage
property, which contains the first x tracks, where x is a number I can't remember. SPTPlaylistSnapshot
具有firstTrackPage
属性,其中包含前x条轨道,其中x是我不记得的数字。 With that first page, you can request additional pages until you have all the tracks. 在第一页中,您可以请求其他页面,直到拥有所有曲目为止。
See the documentation for SPTPlaylistSnapshot
and SPTListPage
for details. 有关详细信息,请参见
SPTPlaylistSnapshot
和SPTListPage
的文档。
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发布此请求:
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)
}
}
}
}
}
}
}
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. 基本上每个人的说法都是正确的,请使用SPTPlaylistSnapshot获取播放列表的曲目,这是我的代码,用于在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()
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.