Integrate Spotify Player iOS(Swift)

Square Infosoft
2 min readJan 3, 2019

--

I am dividing this article in 3 Section:
1. Setup the iOS SDK
2. Fetch Tracks
3. Integrate Spotify Player

Also you can play audio using Spotify player. Need Setup player so first set delegate

class PlayerVC: UIViewController, SPTAudioStreamingPlaybackDelegate, SPTAudioStreamingDelegate {}

Now initialise player with Spotify user. Create SPTAudioStreamingController object and set delegate with current class, set client id which is saved from Spotify Dashboard. We need to check current user access token is valid or not.

if SpotifyPlayer.player == nil {
SpotifyPlayer.player = SPTAudioStreamingController.sharedInstance()
SpotifyPlayer.player!.playbackDelegate = self
SpotifyPlayer.player!.delegate = self
try! SpotifyPlayer.player?.start(withClientId: Spotify.clientId)
SpotifyPlayer.player!.diskCache = SPTDiskCache(capacity: 1024 * 1024 * 64)
SpotifyPlayer.player!.login(withAccessToken: UserDefaults.standard.string(forKey: UserDefault.accessToken) ?? "")
}

There is three delegate methods which get call back from Spotify.

audioStreamingDidLogin
didReceiveError

When you initialise player check Spotify current user access token and get call back in above three methods while communicate with Spotify. Call audioStreamingDidLogin if Spotify player is successfully initialised or call didReceiveError if Spotify player is failed to verify user token or client id. you can track error code or message in didReceiveError function error parameter.

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

Now Spotify Player is ready to play your favorite song….🙂🎼

Now Play song with Spotify, Need to create player object forSPTAudioStreamingController and set delegate to self class like

var player: SPTAudioStreamingController?override func viewDidLoad() {
super.viewDidLoad()
self.player?.delegate = self
}

Now call this method for start playing song and pass Song URI which you want to play. Song URI link is got while we fetching Spotify user tracks URI format is like this: spotify:track:03IxJiB8ZOH9hEQZF5mCNY

self.player?.playSpotifyURI("spotify:track:03IxJiB8ZOH9hEQZF5mCNY", startingWith: 0, startingWithPosition: 0, callback: { (error) in})

If failed to play URI or URI is invalid then get error code or message in completion call back, Also you can play song from specific duration. Seek song duration while playing just callSeek function and set duration where you want to play from and keep in mind duration which you want to set is in Double Data type and in second format.

self.player?.seek(to: 5, callback: { (error) in})

You can pause and resume song using single function just call setIsPlaying function and set change playing status for example if you want to pause song than pass ‘false’ or if you want to resume song than pass ‘true’ just like:

self.player?.setIsPlaying(true, callback: { (error) in})

or if you want to stop player than call stop() function but it can throw error so so it Can be the reason for app crash so need to use error handler do catch.

do {
try self.player?.stop()
} catch {
}

Also you can use Spotify player other features which provided by Spotify like set song queue List, repeat, shuffle, setVolume and other.

Now Spotify Player is ready…🙂

--

--