Is it possible to get the currently playing track in the 1.x apps api?

jerblack picture jerblack · Mar 13, 2014 · Viewed 9.1k times · Source

I am trying to update my Spotify remote control app that is currently using the legacy API to use the new 1.x API. Is it possible using the 1.x API to access information about the currently playing track? models.player.track does not seem to exist anymore (though it's in the documentation).

For the curious, I am using this for my app running in Spotify Desktop which uses websockets to talk with a Python server, which then provides a web interface for phones and tablets to remotely control the instance of Spotify running on the desktop. This works great using the legacy API and I can control playback and get the now playing info from any connected remote. I assume this app is going to stop working at some point soon since Spotify says they are retiring the legacy API. (Unless my assumption that the app will stop working is wrong, then never mind).

Thanks.

Answer

José M. Pérez picture José M. Pérez · Mar 14, 2014

It is possible to access the current playing track loading the track property of the Player.

You would do something like this:

require(['$api/models'], function(models) {

    function printStatus(track) {
        if (track === null) {
            console.log('No track currently playing');
        } else {
            console.log('Now playing: ' + track.name);
        }
    }

    // update on load
    models.player.load('track').done(function(p) {
        printStatus(p.track);
    });

    // update on change
    models.player.addEventListener('change', function(p) {
        printStatus(p.data.track);
    });
});

You have a working example in the Tutorial App named Get the currently playing track.