如何从Spotify获取当前用户的播放列表

时间:2019-03-06 10:37:11

标签: ios swift spotify

我正在尝试与当前用户的播放列表实现Spotify集成,以将其显示在我的表格视图中。我已经集成了登录和访问令牌,一切正常。我通过了堆栈溢出链接:-How to get the list of songs using Spotify in Swift3 iOS?,但对我不起作用。

然后为canonicalUsername进行打印,如下所示,它显示nil值

img = nib.load(example_filename)
data = img.get_fdata()

由于初学者,我什至尝试过此链接Spotify iOS SDK Swift display all (!) playlists (20+),可能对我也不起作用。有什么方法可以获取Spotify的当前用户ID?如何在表格视图中显示当前用户的播放列表?

1 个答案:

答案 0 :(得分:0)

只需浏览youtube上的在线教程:-https://www.youtube.com/watch?v=KLsP7oThgHU&t=1s即可获得2019年的最新版本。

使用Spotify集成+搜索选项+默认Spotify网址下载完整的源代码,并获取当前用户的播放列表并在我们的本机iOS应用中播放 来源:-https://github.com/azeemohd786/Spotify-Demo

根据您的问题,获取打印canonicalUsername或当前用户ID的方法如下,

 SPTUser.requestCurrentUser(withAccessToken: session.accessToken) { (error, data) in
                    guard let user = data as? SPTUser else { print("Couldn't cast as SPTUser"); return }
                    let userID = user.canonicalUserName

                 print(userID!)
                 }   

然后要获取当前用户的播放列表并在您的设备中播放,请先在您的视图控制器中调用SPT代表,然后再调用函数

class PlayVC: UIViewController, SPTAudioStreamingDelegate, SPTAudioStreamingPlaybackDelegate {
func audioStreamingDidLogin(_ audioStreaming: SPTAudioStreamingController) {
        let playListRequest = try! SPTPlaylistList.createRequestForGettingPlaylists(forUser: session.canonicalUsername, withAccessToken: session.accessToken)
        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
                      // self.tableView.reloadData()// if u want to display playlist name and other stuffs like so..
                        SPTAudioStreamingController.sharedInstance().playSpotifyURI("\(playlist.uri!)", startingWith: 0, startingWithPosition: 10) { error in
                            if error != nil {
                                print("*** failed to play: \(error)")
                                return
                            }
                        }
                        }}}

    }
}