How can I add a track to the current play queue in a Spotify app?
3 Answers
You need to create an unnamed playlist to create your own play queue.
function playTracks(tracks, index) {
var pl = new models.Playlist();
for (var i = 0; i < tracks.length; ++i) {
pl.add(tracks[i]);
}
models.player.play(pl.uri, pl.uri, index);
}
-
+1 for doing it with the public API. new Playlist() makes a temporary playlist (at least not one I see in the interface), and playing with playlist as context clears the Play Queue and adds the playlist. Even adding items to the playlist after it started playing works (gets updated in the interface as soon as you play "next")– ClaudeCommented Jun 7, 2012 at 10:43
The current play queue seems to be unavailable. But this snippet may be useful if your purpose is to build a queue...
// Create a name for a temporary playlist.
function temporaryName() {
return (Date.now() * Math.random()).toFixed();
}
function getTemporaryPlaylist() {
var temporaryPlaylist = sp.core.getTemporaryPlaylist(temporaryName());
sp.trackPlayer.setContextCanSkipPrev(temporaryPlaylist.uri, false);
sp.trackPlayer.setContextCanRepeat(temporaryPlaylist.uri, false);
sp.trackPlayer.setContextCanShuffle(temporaryPlaylist.uri, false);
return temporaryPlaylist;
}
var tpl = getTemporaryPlaylist();
tpl.add(trackUri);
tpl.add(track2Uri);
//...
sp.trackPlayer.playTrackFromContext(tpl.uri, 0, "", {
onSuccess: //...
onError: //...
onComplete: //...
});
Nothing in the Apps API reference suggests that it is possible. There is no mention of how to do this in any of the apps in the preview build either. The conclusion has to be that doing this is not currently supported.