spotify/cocoalibspotify — Issue #29: First track loads slow, second track never gets loaded
Author: RoelandW
Created: 2011-12-28 15:52:35+00:00
State: open

Hi,

I have two problems (maybe they have something to do with each other). I am working on an iPhone app and using the latest code (master branch). I'm developing on device (the simulator crashes on libspotify for some reason) with iOS4.2.1 and iOS5 (latest).

First of all, the first track loads really slow. The whole UI hangs and is unresponsive, but after 5 to 10 seconds the track is played. I have seen some issues here on Github that look similar, but i have no clue why this is happening.

The other problem occurs when I want to play another track. The app gets in a loop and the track is never loaded. I can continue playing the first song, but the second song is never plays (rarely it does play).

This is the play code (most of it is copy-pasted from your examples):

```
- (void)playTrack
{
// make sure wo don't start multiple requests
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(playTrack) object:nil];

NSURL *trackURL = self.currentSpotifyItem.spotifyURL;

// play spotify track
if (trackURL)
{
SPTrack *track = [[SPSession sharedSession] trackForURL:trackURL];

if (track != nil)
{
if (!track.isLoaded)
{
// Since we're trying to play a brand new track that may not be loaded,
// we may have to wait for a moment before playing. Tracks that are present
// in the user's "library" (playlists, starred, inbox, etc) are automatically loaded
// on login. All this happens on an internal thread, so we'll just try again in a moment.
[self performSelector:@selector(playTrack) withObject:nil afterDelay:0.1];
return;
}

NSError *error = nil;

if (![self.playbackManager playTrack:track error:&error])
{
// some error occured
}

// track is playing
return;
}
}
}
```

If another track is requested by the user I perform the following lines of code as well:

```
if ([self.playbackManager isPlaying])
{
[[SPSession sharedSession] unloadPlayback]; // tried with and without
[self.playbackManager setIsPlaying:NO];
}
```

It seems like I have to stop the current track somehow first, or set the session, but I have no idea how.

I have no idea where to look further. Is this a common problem?