3
votes

I am currently using the Web Player SDK / Web Api to control spotify playback in my application. The current issue I am running into is that whenever I play a song, I cannot detect the end of the playback of the song because the

state.track_window.next_songs

when listening through

player.addListener('player_state_changed', state => { });

always has items in the "Play Queue", which causes detection almost impossible.

this: state && state.paused && state.context && state.context.metadata && state.context.metadata.uri == null typically works for, however in the case that state.track_window.next_songs has anything in it, paused is never triggered.

I am wondering if anyone has a solution for this, or if there is a way to clear our the play queue for a specific spotify player instance?

Thanks!

1

1 Answers

0
votes

I'm not sure if I understood correctly, but if you want to detect when a song finished playing you can have an object called currentlyPlaying with the ID of the currently playing song and then when there is a state update, check the ID of the state.current_track. If it's different from your currentlyPlaying.id then it means the song finished and the next one started.

----- EDIT ------

There is a way to have the next_tracks array empty. If you play songs using the "uris" body parameter you can add an array of songs to play and in the end of it the next_tracks array is empty. See this screenshot: enter image description here

To achieve this take a look at the Spotify API: https://developer.spotify.com/console/put-play/?device_id=&body=%7B%20uris%3A%20%5Bspotify%3Atrack%3A3BQHpFgAp4l80e1XslIjNI%5D%20%7D

I am only using the "uris" parameter not the "context_uri" and I am passing only 1 song in the array. After the song is played nothing is played after. enter image description here