【问题标题】:I can't play music with Spotify iOS SDK with Swift 4.0我无法使用 Swift 4.0 的 Spotify iOS SDK 播放音乐
【发布时间】:2018-06-05 02:35:34
【问题描述】:

我在使用 Spotify SDK 时遇到了困难,我正确地遵循了每一步,但我无法在我的项目中使用我的高级帐户播放音乐。没有错误或崩溃,我的应用程序将我定向到 Spotify 登录页面,在 Facebook 登录后,它再次将我带回我的应用程序。昨天我会得到“登录”打印,但今天我不能。我正在尝试在登录后播放歌曲,也可以手动播放,如下所示。我想知道,我有幸找到答案吗?

override func viewDidLoad() {
    super.viewDidLoad()
    self.spotify()
    NotificationCenter.default.addObserver(self, selector: #selector(GeneralNewsViewController.updateAfterFirstLogin), name: NSNotification.Name(rawValue: "SpotifySession"), object: nil)
 }

func spotify() {
    // insert redirect your url and client ID below
    auth.redirectURL     = URL(string: "correctCallbackURl")
    auth.clientID        = "correctClientID"
    auth.requestedScopes = [SPTAuthStreamingScope, SPTAuthPlaylistReadPrivateScope, SPTAuthPlaylistModifyPublicScope, SPTAuthPlaylistModifyPrivateScope]
    loginUrl = auth.spotifyWebAuthenticationURL()
}

func initializaPlayer(authSession:SPTSession){
    if self.player == nil {
        self.player = SPTAudioStreamingController.sharedInstance()
        self.player!.playbackDelegate = self
        self.player!.delegate = self
        try! player?.start(withClientId: auth.clientID)
        self.player!.login(withAccessToken: authSession.accessToken)

    }
}


@objc func updateAfterFirstLogin () {

    loginButton.isHidden = true
    let userDefaults = UserDefaults.standard

    if let sessionObj:AnyObject = userDefaults.object(forKey: "SpotifySession") as AnyObject? {

        let sessionDataObj = sessionObj as! Data
        let firstTimeSession = NSKeyedUnarchiver.unarchiveObject(with: sessionDataObj) as! SPTSession

        self.session = firstTimeSession
        initializaPlayer(authSession: session)
        self.loginButton.isHidden = true
        // self.loadingLabel.isHidden = false

    }

}

func audioStreamingDidLogin(_ audioStreaming: SPTAudioStreamingController!) {
    // after a user authenticates a session, the SPTAudioStreamingController is then initialized and this method called
    print("logged in")
    self.player?.playSpotifyURI("spotify:track:4aDLPXlzHZm26GppvRwms8", startingWith: 0, startingWithPosition: 0, callback: { (error) in
        if (error != nil) {
            print("playing!")
        } else {
            print(error?.localizedDescription)
        }
    })
}

@objc func play() {
    player?.playSpotifyURI("spotify:track:4aDLPXlzHZm26GppvRwms8", startingWith: 0, startingWithPosition: 0, callback: { (err) in
        if err != nil {
            print(err?.localizedDescription)
        } else {

        }
    })
}

class AppDelegate: UIResponder, UIApplicationDelegate ,UNUserNotificationCenterDelegate{ 
    auth.redirectURL = URL(string: "correctCallbackURl")
    auth.sessionUserDefaultsKey = "current session"}

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    // 2- check if app can handle redirect URL
    if auth.canHandle(auth.redirectURL) {
        // 3 - handle callback in closure
        auth.handleAuthCallback(withTriggeredAuthURL: url, callback: { (error, session) in
            // 4- handle error
            if error != nil {
                print("error!")
            }
            // 5- Add session to User Defaults
            let userDefaults = UserDefaults.standard
            let sessionData = NSKeyedArchiver.archivedData(withRootObject: session!)
            userDefaults.set(sessionData, forKey: "SpotifySession")
            userDefaults.synchronize()
            // 6 - Tell notification center login is successful
            NotificationCenter.default.post(name: Notification.Name(rawValue: "loginSuccessfull"), object: nil)
        })
        return true
    }
    return false
}}

【问题讨论】:

    标签: ios swift xcode spotify swift4


    【解决方案1】:

    您似乎订阅了错误的通知,您订阅了“SpotifySession”但发布了“loginSuccessfull”

    这个answer 将通过简单的事件枚举来帮助你在未来犯这种错误。

    良好的调试也是关键,您可能已经通过一些断点解决了这个问题,以查看哪里出错了。

    干杯

    【讨论】:

      猜你喜欢
      • 2014-08-16
      • 2016-11-18
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多