Type in your question below and we'll check to see what answers we can find...
Loading article...
Submitting...
If you couldn't find any answers in the previous step then we need to post your question in the community and wait for someone to respond. You'll be notified when that happens.
Simply add some detail to your question and refine the title if needed, choose the relevant category, then post.
Before we can post your question we need you to quickly make an account (or sign in if you already have one).
Don't worry - it's quick and painless! Just click below, and once you're logged in we'll bring you right back here and post your question. We'll remember what you've already typed in so you won't have to do it again.
How does one update the progress bar?
Are we supposed to poll the API (ttps://api.spotify.com/v1/me/player/)
every X milliseconds and update the UI?
Solved! Go to Solution.
Just to clarify, on my snippet in my 1st message. The `state.position` is derived from: event-player-state-changed
That event is fired when the song changes, play/pause, or at random intervals.
When that event fires, it will update / correct the position. And simultaneously the timer will add progress to it.
I have had that running for a quite a while, and can't really get it out-of-sync.
Not sure what SDK you are using. But on the Web Playback SDK, the player will update it's state whenever user interacts with it or the song changes. Then I created my own loop to just mimic the progress of the song if the song is playing - no need to poll. And it will update itself when the player state updates.
$: position = state.position;
onInterval(() => position += state.paused ? 0 : 300, 300);
Above snippet is svelte, but the logic is:
Above state object is: WebPlaybackState Object
So what i'm doing right now is, after API call to play the song is made, I start a setInterval to update the progress bar every 100ms.
But setInterval isn't exactly reliable in since JS is single-threaded. Was wondering if there's a more accurate, fail-safe way ๐
Looks like this is the way to go. thank you!
I do a bit of both. I guess where the progress should be based on an interval, then periodically get the actual progress and update. About every 5 seconds currently but looking to tune the interval logic to consider actual start time and current time, things like that
Just to clarify, on my snippet in my 1st message. The `state.position` is derived from: event-player-state-changed
That event is fired when the song changes, play/pause, or at random intervals.
When that event fires, it will update / correct the position. And simultaneously the timer will add progress to it.
I have had that running for a quite a while, and can't really get it out-of-sync.
Ooohhh yes. this helped tremendously. thanks for clarifying that!
Hey there you, ย Yeah, you!ย 😁 ย Welcome - we're glad you joined the Spotify Community! ย While you here, let's have a fun game and getโฆ