Spotify轨道错误

时间:2019-07-10 11:36:41

标签: ios spotify

我使用iOS SDK。我有两个按钮来播放歌曲

凯蒂·佩里(Katy Perry)的曲目《烟花》中的一首歌

appRemote.playerAPI?.play("spotify:track:4lCv7b86sLynZbXhfScfm2") { (restult, error) in }

罗比·威廉姆斯(Robbie Williams)的履带角度第二

appRemote.playerAPI?.play("spotify:track:1M2nd8jNUkkwrc1dgBPTJz") { (restult, error) in }

我在它们之间切换,它可以正常工作。

但是,例如,如果罗比·威廉姆斯(Robbie Williams)的曲目结束而不是新歌播放,则以史努比狗(Snoop Dog)的歌曲为例。我切换到凯蒂(Katy),听完她的歌,开始新歌,例如Metallica的歌。现在,当我尝试在Katy和Robbie之间切换时,我在Metallica和Snoop Dog之间切换。 我做错了什么?

我尝试过:appRemote.playerAPI?.setRepeatMode(.track) { (restult, error) in },但没有帮助。

完整来源:


let SpotifyClientID = "xxx"
let SpotifyRedirectURL = URL(string: "xxx://callback")!

class SpotifyManager: NSObject {
    static var shared = SpotifyManager()

    static var playerStateDidChange: (()->())?

    static var configuration = SPTConfiguration(
        clientID: SpotifyClientID,
        redirectURL: SpotifyRedirectURL
    )

    static var sessionManager: SPTSessionManager = {
        if let tokenSwapURL = URL(string: "https://xxx.glitch.me/api/token"),
            let tokenRefreshURL = URL(string: "https://xxx.glitch.me/api/refresh_token") {
            configuration.tokenSwapURL = tokenSwapURL
            configuration.tokenRefreshURL = tokenRefreshURL
        }

        if currentTrackId.count != 0 {
            configuration.playURI = "spotify:track:\(SpotifyManager.currentTrackId)"
        } else {
            configuration.playURI = ""
        }

        let manager = SPTSessionManager(configuration: configuration, delegate: shared)
        return manager
    }()

    static var appRemote: SPTAppRemote = {
        let appRemote = SPTAppRemote(configuration: configuration, logLevel: .debug)
        appRemote.delegate = shared
        return appRemote
    }()

    static var currentTrackId = ""

    static func play(track_id: String) {
        currentTrackId = track_id
        SpotifyManager.playerStateDidChange?()
        if sessionManager.session == nil || appRemote.isConnected == false {
            let requestedScopes: SPTScope = [.appRemoteControl]
            if #available(iOS 11.0, *) {
                sessionManager.initiateSession(with: requestedScopes, options: .default)
            }
        } else {
            appRemote.playerAPI?.play("spotify:track:\(track_id)") { (restult, error) in
            }
        }
    }
    static func pause() {
        currentTrackId = ""
        SpotifyManager.playerStateDidChange?()
        appRemote.playerAPI?.pause { (restult, error) in

        }
    }
    static func isPlaying(track_id: String) -> Bool {
        return SpotifyManager.currentTrackId == track_id
    }
}

extension SpotifyManager: SPTSessionManagerDelegate {
    func sessionManager(manager: SPTSessionManager, didInitiate session: SPTSession) {
        NSLog("didInitiate \(session)")

        SpotifyManager.appRemote.connectionParameters.accessToken = session.accessToken
        SpotifyManager.appRemote.connect()
    }
    func sessionManager(manager: SPTSessionManager, didFailWith error: Error) {
        NSLog("didFailWith \(error)")
    }
    func sessionManager(manager: SPTSessionManager, didRenew session: SPTSession) {
        NSLog("didRenew \(session)")
    }
}

extension SpotifyManager: SPTAppRemoteDelegate, SPTAppRemotePlayerStateDelegate {
    func appRemoteDidEstablishConnection(_ appRemote: SPTAppRemote) {
        NSLog("didEstablishConnection")
        SpotifyManager.appRemote.playerAPI?.delegate = self
        SpotifyManager.appRemote.playerAPI?.subscribe { (result, error) in
            if let error = error {
                NSLog(error.localizedDescription)
            }
        }
        SpotifyManager.playerStateDidChange?()
    }
    func appRemote(_ appRemote: SPTAppRemote, didDisconnectWithError error: Error?) {
        NSLog("didDisconnectWithError")
        SpotifyManager.currentTrackId = ""
        SpotifyManager.appRemote.disconnect()
        SpotifyManager.playerStateDidChange?()
    }
    func appRemote(_ appRemote: SPTAppRemote, didFailConnectionAttemptWithError error: Error?) {
        NSLog("didFailConnectionAttemptWithError")
        SpotifyManager.currentTrackId = ""
        SpotifyManager.appRemote.disconnect()
        SpotifyManager.playerStateDidChange?()
    }
    func playerStateDidChange(_ playerState: SPTAppRemotePlayerState) {
        NSLog("\(playerState.track.name)")
        SpotifyManager.playerStateDidChange?()
    }
}


0 个答案:

没有答案