Jasper Kuperus
Jasper Kuperus

Reputation: 1692

Spotify Apps API: Detect offline mode

Is there some way to detect whether Spotify is in Offline mode? I am currently developing an app which clearly depends on a working internet connection. I would like to be able to detect whether there is a connection to the internet at all and if not, show an error message.

I have found some general solutions for javascript which did not seem to work, like:

var online = navigator.onLine;

Upvotes: 3

Views: 1388

Answers (4)

Per Quested Aronsson
Per Quested Aronsson

Reputation: 12120

This is how you do it in the newer (2013+) 1.0 API:

 models.session.addEventListener('change:online',function({
    console.log('online =',models.session.online);
});

See https://developer.spotify.com/docs/apps/api/1.0/api-models-session.html for more info.

Upvotes: 0

Mark
Mark

Reputation: 632

For Apps API V1 you would do something along the lines of...

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

  // add a listener to pick up any changes in on / offline state
  models.session.addEventListener('change', changeOffline);

  function changeOffline(){
    var online = models.session.online; // returns true / false
    if(!online){
      // show offline message etc...
    } else {
      // hide offline message etc...
    }
  }
});

https://developer.spotify.com/docs/apps/api/1.0/models-session.html

Upvotes: 0

pompa
pompa

Reputation: 688

You can detect if the client is offline by accessing the state in the session object.

https://developer.spotify.com/technologies/apps/docs/a5a59ca068.html

What you need to do is listen to the state change using the observer and look for the OFFLINE state.

DISCONNECTED: 2
DUMMY_USER: 4
LOGGED_IN: 1
LOGGED_OUT: 0
OFFLINE: 3

var sp = getSpotifyApi(1);
var models = sp.require('sp://import/scripts/api/models');

models.session.observe(models.EVENT.STATECHANGED, function() {
    console.log(models.session.state);
});

Upvotes: 5

sparkFinder
sparkFinder

Reputation: 3404

http://developer.spotify.com/en/libspotify/docs/group__session.html

Checking

sp_connectionstate 

for the enum value:

SP_CONNECTION_STATE_OFFLINE

Should get you what you want.

Upvotes: 0

Related Questions