I've followed the accepted answer from this question: Spotify App API: Play specific track from album
However, it doesn't work for me. Spotify is still playing the first track from the album.
I've had a look at view.js and I can see why it's playing the first track:
if (play) {
if (m.player.context !== this._context.uri) {
m.player.play(this._context.get(0), this._context);
}
My code is below, any thoughts/suggestions?
var track = models.Track.fromURI(item.SpotifyUrl, function(track) {
var album = models.Album.fromURI(track.album.uri, function(album) {
var trackPlayer = new views.Player();
var t = album.data.tracks[track.data.trackNumber-1];
trackPlayer.track = t;
album.get = function() {
console.log(track.name); // I know this is being called
return track;
}
trackPlayer.context = album;
var $player = $(trackPlayer.node);
});
});
Thanks in advance.
UPDATE:
I had a further look at the api and within models.js I found this:
if (index === -1) {
...
else if (context instanceof Album) {
numTracks = context.tracks.length;
while (i < numTracks) {
if (context.tracks[i].data.availableForPlayback) {
track = context.tracks[i];
index = i;
break;
} else {
i++;
}
The following method call:
if (play) {
if (m.player.context !== this._context.uri) {
m.player.play(this._context.get(0), this._context);
}
omits the 'index' parameter. Therefore the player will always play the first available track! To get around this I've added the following:
models.player.play = function(track, opt_context, opt_index) {
return this.playTrackWithContext(track,opt_context,track.data.trackNumber-1);
};
It seems a bit of a messy workaround but until Spotify update their code it looks like the only way to get around it. Unless, I've completely missed something and there is an easier way?