New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to detect when a song ends #43
Comments
Maybe we can just subscribe to player state and check when we'll get new track with new track id or name. |
The problem is I would like to play something before the next track starts |
I can suggest you to research example repo of app-remote. In this example you can find TrackProgress bar, that we can use for our needs. |
I'm really curious, is there any solution to this? Was hoping it was going to be relatively straight forward by subscribing to a state, but I'm starting to see it isn't that easy huh? 👎 |
There is a similar issue for the web-playback-sdk: spotify/web-playback-sdk#35 A track seems to be ended if the position is 0 and if it is paused. However this can also appear in the beginning of a track, so you need to check whether it’s not this starting-state. private var trackWasStarted = false
// get the player state
spotifyAppRemote.playerApi.subscribeToPlayerState().setEventCallback {
handleTrackEnded(it)
}
// ...
private fun handleTrackEnded(playerState: PlayerState) {
setTrackWasStarted(playerState)
val isPaused = playerState.isPaused
val position = playerState.playbackPosition
val hasEnded = trackWasStarted && isPaused && position == 0L
if (hasEnded) {
trackWasStarted = false
// ... do whatever you want to do if track ended
}
}
private fun setTrackWasStarted(playerState: PlayerState) {
val position = playerState.playbackPosition
val duration = playerState.track.duration
val isPlaying = !playerState.isPaused
if (!trackWasStarted && position > 0 && duration > 0 && isPlaying) {
trackWasStarted = true
}
} |
I agree with @paulgoetze, an event is generated "most" times with the song that has ended along with the position set to 0. Have you come across this issue aswell @paulgoetze, or has your solution provided a suitable work around for your needs? Also during your tests has auto-play been enabled or disabled in the spotify app? |
@KyleGonzalez I also had issues when auto-play was enabled (see #90) and did not find a work-around for this. Also, I haven't tried whether the event is reliably generated when queued tracks end. |
Hi, there's no specific functionality for this, but have you tried listening to Player State and checking |
but how would one go about doing this without a timer running in parallel to sync playback position? cause it seems pretty janky to do it that way... |
I found a workaround for getting app-remote to do what you want at the end of a song, based on paulgoetze suggestion above. (granted it is a bit messy, and sometimes you'll hear another song play for a blip before the correct action kicks in)
This is not an exact substitute for reliable end-of-song event generation, but hopefully it will help someone struggling with this issue. |
Is there a way we can detect when a song ends?
I'm trying to create some sort of custom queue, as I couldn't find any way to modify the queue, any pointers as to how this can be achieved?
Thanks
The text was updated successfully, but these errors were encountered: