spotify / web-playback-sdk

This issue tracker is no longer used. Join us in the Spotify for Developers forum for support with the Spotify Web Playback SDK ➡️ https://community.spotify.com/t5/Spotify-for-Developers/bd-p/Spotify_Developer
106 stars 7 forks source link

How to get the current audio progress over time? #86

Open 5haw4 opened 5 years ago

5haw4 commented 5 years ago

TLDR: In the Web playback SDK I'm trying to achieve the same functionality as if I were to use the DOM event "timeupdate" and "audio.currentTime" in order to update over time my progress text and show the current progress of the audio.

I can't find a simple way of getting the current progress of the audio that's being played in order to update my UI accordingly, and for some reason the web SDK doesn't generate / create an accessible audio tag in the HTML, so I can't just use the DOM listener "timeupdate" and get its "currentTime" in order to get the current progress of the audio every time it's changed...

I tried to use intervals of 1000 milliseconds (also tried less) and in that interval I get the SDK's current state with "player.getCurrentState()" but for some reason sometimes when you stop playing or when you disconnect the instance (usually when you open up Spotify's app on the phone), the SDK still keeps on increasing the "state.position" even though the music is no longer being played on the WEB nor anywhere else, and it's increasing the "state.position" and never stops even when its value is greater than the "state.duration" value...

I'd love to get some help with that bug / unexpected behavior, thanks in advance :)

tobika commented 5 years ago

My approach is to also use an interval and correct the time difference with the spotify state that arrives every 30sec via the on_state_change, if the user pauses the playback you will also immediately receive an state change event for this

If you call getCurrentState() all the time you could even get problems with race conditions between the http calls which might be the explanation for the wrong state you sometimes have

5haw4 commented 5 years ago

My approach is to also use an interval and correct the time difference with the spotify state that arrives every 30sec via the on_state_change, if the user pauses the playback you will also immediately receive an state change event for this

If you call getCurrentState() all the time you could even get problems with race conditions between the http calls which might be the explanation for the wrong state you sometimes have

Can u share your solution? (the few lines of code that responsible on tracking the progress)

BTW does it also happen to you, that the first couple of "player_state_changed" the player isn't paused but audio isn't playing, and the position is counting up, and after a couple of seconds there's another "player_state_changed" callback that fixes it (meaning that it's paused and its position is 0) What I mean is: I log every "player_state_changed" to the console for testing and when I load the page for the first time it right away start printing to the log the position and if it's paused or not according to the state that I get from the "player_state_changed" callback, and for the first couple of callback every time I start a new queue it start counting up the position even though the audio isn't playing, and after few seconds when the audio is actually playing it fixes itself and starts over from the actual position of the audio...