Search code examples
iosswiftspotify

iOS Spotify Streaming SDK - Error: Context Failed 1006


I am using the 'old' Streaming SDK because the new one does not meet my needs. I got it working pretty well however for some tracks I can't start the playback. I am printing the messages to the console and this is the output I got:

is playing: true

didReceivePlaybackEvent: SPPPlaybackNotifyPlay

didReceivePlaybackEvent: SPPPlaybackNotifyAudioFlush

didReceivePlaybackEvent: SPPPlaybackNotifyMetadataChanged

didReceivePlaybackEvent: SPPPlaybackNotifyAudioFlush

didReceivePlaybackEvent: SPPPlaybackNotifyAudioDeliveryDone

is playing: false

didReceivePlaybackEvent: SPPPlaybackNotifyPause

*** Received error: (1006)Context failed

didReceivePlaybackEvent: SPPPlaybackNotifyMetadataChanged

Those messages are coming from the delegate methods:

func audioStreaming(_ audioStreaming: SPTAudioStreamingController, didReceiveError error: Error) {

    let nsError = error as NSError
    print("*** Received error: (\(nsError.code))\(nsError.localizedDescription)")
}

    func audioStreaming(_ audioStreaming: SPTAudioStreamingController, didChangePlaybackStatus isPlaying: Bool) {
        print("is playing: \(isPlaying)")
    }

    func audioStreaming(_ audioStreaming: SPTAudioStreamingController, didReceive event: SpPlaybackEvent) {
        print("didReceivePlaybackEvent: \(event)")
    }

I believe I initialize my player correctly, it's taken from the official example app from Spotify's repo:

do {
    try player.start(withClientId: clientID, audioController: nil, allowCaching: true)
    player.delegate = self
    player.playbackDelegate = self
    player.diskCache = SPTDiskCache(capacity: 1024 * 1024 * 64)
    player.login(withAccessToken: session.accessToken)
    setBitrate(preferredBitrate)
} catch let error {
    delegate?.didReceiveError(self, error: error, priority: .test)
    closeSession()
}

I looked up the documentation of the errors but there is no error with code 1006 or name SPTErrorContextFailed

for err in [
    SPTErrorCodeFailed,
    SPTErrorCodeNoError,
    SPTErrorCodeInitFailed,
    SPTErrorCodeUnsupported,
    SPTErrorCodeNeedsPremium,
    SPTErrorCodeNullArgument,
    SPTErrorCodeUninitialized,
    SPTErrorCodeFailed,
    SPTErrorCodeBadCredentials,
    SPTErrorCodeInvalidArgument,
    SPTErrorCodeNotActiveDevice,
    SPTErrorCodeWrongAPIVersion,
    SPTErrorCodeTrackUnavailable,
    SPTErrorCodeApplicationBanned,
    SPTErrorCodeGeneralLoginError,
    SPTErrorCodeTravelRestriction,
    SPTErrorCodePlaybackRateLimited,
    SPTErrorCodeGeneralPlaybackError
    ] {
        if nsError.code == err {
            print(err)  // It is not printed because there is no error 
                        // 1006 declared
        }
}

As I said, all of this works most of the time, but for some tracks I get the error and they can't be played. I also tested the track with all available bitrates but that didn't change a thing.

What could be the issue here?


Solution

  • I figured it out. Some tracks were not available in my region, but my back-end did not correctly recognized those tracks. I fixed that and now only playable tracks are passed to the player and the problem does not occur anymore.

    If a track can not be played it will be greyed out in the Spotify desktop app