3

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.

jerblack
  • 1,203
  • 15
  • 15

1 Answers1

1

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.

José M. Pérez
  • 3,199
  • 22
  • 37
  • Excellent! Thank you. That should be more than enough to get me going. Much appreciated. – jerblack Mar 14 '14 at 15:09
  • 5
    For anyone else who'll chance upon this question later, the apps API has been discontinued and therefore this answer no longer works. I'm currently looking for an efficient way to solve this problem since the API doesn't seem to give a way to get the current track being listened to. If I find one I will update the comment. – kiriappeee Mar 23 '15 at 07:13
  • 3
    Is there a good solution now? Would be very interested in a way to get the current song. – fuuman Jul 09 '15 at 14:30
  • There's a feature request (https://github.com/spotify/web-api/issues/12) for their web API, but they did not implement this feature so far. – endowzoner Jan 06 '16 at 21:33
  • There are some (pretty dirty) workarounds written here: http://stackoverflow.com/questions/33883360/get-spotify-currently-playing-track – Nearoo Nov 05 '16 at 14:01