2

I'm trying to create cool visualization to music using the Spotify Web API(https://developer.spotify.com/documentation/web-api/reference/).

What I'm trying, is to first fetch what the user is playing, what's their progress and then I fetch the track analysis as well.

Fetching the currently played song is possible on this endpoint: https://developer.spotify.com/documentation/web-api/reference/player/get-the-users-currently-playing-track/

The important stuff for me from the response is basically this:

{
  "id": H7sbas7Gda98sh...., //ID of the song
  "timestamp": 1490252122574, //unix timestamp when the data is fetched
  "progress_ms": 42567 //The progress of the track in milliseconds
}

Obviously some time elapses between the request and the time I parse the response in my application. So the plan is that I synchronize the music this way:

const auto current_time = get_current_unix_timestamp();
const auto difference = current_time - timestamp; //the timestamp that is in the response
const offset = progress_ms + difference; //the progress_ms that is in the response

The theory is cool but it seems like the clock between the servers and on my system is not synchronized because I usually get values like -1638 for difference which is obviously not good because that would mean that I parsed the data sooner than it was fetched.

So my question is that what options do I have to synchronize my clock to Spotify servers? If it's not possible what options do I have to be able to synchronize the music properly? I couldn't find anything in Spotify documentation, although it should be possible because there are already some existing applications that do the same what I'm trying to do(e.g.: https://www.kaleidosync.com/)

johnyka
  • 399
  • 3
  • 15
  • 2
    I'm not sure how possible this would be without a lot of api calls which isn't very practical. Could you make the call, take the time, count locally, re-check at regular intervals to sync your local timer..how effective that would be depends what you're aiming to do with the progress I guess. – scgough Nov 27 '19 at 16:43
  • Yes, I'm experimenting with something like this :) – johnyka Dec 03 '19 at 14:38

1 Answers1

1

It seems that synchronization is not currently practical, because while the docs say that the "timestamp" field signifies when the API gave you the data, it actually does not do what it says because of some issue on their side: https://github.com/spotify/web-api/issues/1073

Instead the timestamp seems to change only when there is a new song starting, pause, or seek. This means that as it is, we cannot know when the API response was generated.

Bemmu
  • 17,849
  • 16
  • 76
  • 93
  • Also, they don't offer any way for realtime player state updates at the moment: https://github.com/spotify/web-api/issues/492 – dAm2K Apr 06 '20 at 00:04
  • 1
    Yeah, I've been following that...It's been almost 3 years. I kind of lost hope on that one – johnyka Apr 06 '20 at 21:13
  • You can use the Web Playback SDK. I think that might be better option, although its not available on all platforms – sj123 Jan 31 '21 at 04:05