我无法使用Swift 4.0的Spotify iOS SDK播放音乐

问题描述 投票:1回答:1

我在使用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个回答
2
投票

看起来您订阅了错误的通知,您订阅了“Spotify Session”但发布了“loginSuccessfull”

这个answer将通过以简单的方式为事件设置枚举来帮助您在将来犯这种错误。

良好的调试也很关键,你可能已经用一些断点解决了这个问题,看看你哪里出错了。

干杯

© www.soinside.com 2019 - 2024. All rights reserved.