5

How can I add a track to the current play queue in a Spotify app?

the
  • 21,007
  • 11
  • 68
  • 101
user507410
  • 512
  • 4
  • 12

3 Answers3

4

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);
}
msfeldstein
  • 1,080
  • 1
  • 9
  • 18
  • +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") – Claude Jun 07 '12 at 10:43
2

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: //...
            });
Juan Riaza
  • 1,618
  • 2
  • 16
  • 35
0

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.

Ivan Navarrete
  • 536
  • 1
  • 5
  • 14