【问题标题】:How to get current user's playlist from Spotify如何从 Spotify 获取当前用户的播放列表
【发布时间】:2019-07-27 23:53:09
【问题描述】:

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

然后打印 canonicalUsername 如下,它显示 nil 值

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

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

【问题讨论】:

  • 您是否尝试过从会话中获取规范用户名? SPTAuth.defaultInstance().session.canonicalUsername? (假设这是使用他们的旧 SDK)
  • @Samantha,我会试试这个,如果你可以,请分享获取当前用户播放列表的代码并在 tableview 中显示。
  • 一旦你得到这个工作,你试图让它在tableView中显示?
  • @Samantha,我设法像代码一样打印播放列表 // if let playlist = playList as? SPTPartialPlaylist { print(playlist.uri!) // 播放列表 uri。我怎么能使用这个 uri 并在 AVPlayer 中播放..
  • 您的问题不断变化。我建议您搜索 Spotify/Swift 教程,然后,如果仍未解决,请使用您已经尝试过的代码发布一个特定问题。假设您使用的是 github.com/spotify/ios-streaming-sdk 的 sdk,框架内的标头记录得很好,您会发现 Spotify 本身实际上为您提供了流媒体播放器。网上有很多不错的教程和之前提出的关于 SO 的问题,它们会让您达到一个至少可以尝试解决问题的地步,我们可以为您提供帮助。

标签: ios swift spotify


【解决方案1】:

只需通过 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 Delegate,然后调用函数,

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
                            }
                        }
                        }}}

    }
}

【讨论】:

  • 感谢您分享教程和源代码。太棒了...虽然有效....解决了我的问题。
  • @Aleesha,只需通过 youtube 教程链接,然后使用源代码来获取你的东西。很高兴听到它在 4 u 中起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
  • 1970-01-01
  • 2014-10-28
  • 1970-01-01
  • 1970-01-01
  • 2016-02-26
相关资源
最近更新 更多