Spotify Playback Switches to Different Songs After Track Completion

I’m working with the Spotify iOS SDK and having trouble with track switching. I set up two buttons that should play specific songs.

The first button plays Ed Sheeran’s “Shape of You”:

spotifyRemote.playerAPI?.play("spotify:track:7qiZfU4dY1lWllzX7mPBI3") { (response, err) in }

The second button plays Taylor Swift’s “Shake It Off”:

spotifyRemote.playerAPI?.play("spotify:track:5ghIJDpPoe3CfHMGu71E6T") { (response, err) in }

Initially everything works fine when I switch between these two tracks. However, when I let Ed Sheeran’s song finish, Spotify automatically starts playing a different track like Drake. Then when I let Taylor Swift’s song finish, another random track like Adele starts playing.

Now when I press my buttons, instead of getting Ed Sheeran and Taylor Swift, I get Drake and Adele - the tracks that played after the original ones ended.

I tried using spotifyRemote.playerAPI?.setRepeatMode(.track) but this doesn’t solve the problem. How can I make sure my buttons always play the exact tracks I want, regardless of what Spotify played automatically after those tracks ended?

yeah, sounds like there’s some queuing issues. maybe try spotifyRemote.playerAPI?.clearQueue() before each play. and instead of calling play for each track, pausing and resuming might give you more control. good luck with it!

This happens because Spotify’s autoplay kicks in after your tracks finish, and the SDK grabs the current playback context instead of your specific track URI. I hit the same issue building a music app last year. What worked for me was creating single-track playlists for each song instead of playing tracks directly. Use spotifyRemote.playerAPI?.play("spotify:playlist:your_playlist_id") after adding one track to a temp playlist. This stops Spotify from continuing with its own recommendations and keeps your buttons pointing to the right songs. You can also check the current track state before playing, explicitly stop playback first, then call your play method.