Skip to content

connect: let play start again once playback has stopped - #1867

Open
stefanbd wants to merge 3 commits into
librespot-org:devfrom
stefanbd:connect-rewind-at-end-of-queue
Open

stefanbd wants to merge 3 commits into
librespot-org:devfrom
stefanbd:connect-rewind-at-end-of-queue

Conversation

@stefanbd

@stefanbd stefanbd commented Nov 11, 2026 •

Copy link
Copy Markdown

Problem

When playback stops, Spirc::play and Spirc::play_pause do nothing, while a Connect client's resume starts it again. The common case is the end of the queue: when the last track ends, or next is pressed on it, with nothing left to play, handle_next calls handle_stop, which stops the player, unloads the track and rewinds the state to the first track. The device is published paused on that first track, and a resume from a Connect client on a stopped device loads it (Resume(_) if Stopped => load_track(true, 0)). But an application's own controls, such as media keys, go through Spirc::play/play_pause, and handle_play only resumes a paused or paused-loading player. I ran into this with a headless player: after an album finished, its media keys did nothing until a Spotify app started something new.

Change

handle_play now starts the current track again when playback has stopped, with load_track(true, 0), which is what the Resume arm did; that arm now just calls handle_play. handle_play_pause plays in every state but playing and loading to play, so it takes the same path. handle_play and handle_play_pause return the load's Result, which handle_command passes on as it does for Prev and Next.

Nothing else changes: playback still stops at the end of the queue, with the same player events, and nothing is loaded until play is pressed. It applies to every way playback stops, so play also starts again after handle_prev stops at the start of the first track, or after a track that failed to load stopped the queue. With no current track, load_track stops as before.

An earlier revision of this PR instead loaded the first track paused at the end of the queue. A review found that this changed the --onevent events there, needed extra handling so that a queue of tracks that can't load isn't walked forever, and could still mark a context's tracks unavailable when the network was down as it ended. This version has none of that.

Test

spirc::tests adds a small SpircTask fixture (these are the first tests in spirc.rs): an active device with a real player, which needs no connection, and a three-track context of local files that aren't on disk. play_starts_the_queue_again_once_it_has_ended lets the last track end, then calls handle_play, and in a second run handle_play_pause, and requires the first track to be current and loading to play at 0. It fails on dev, where both do nothing.

#1870 adds the same fixture to a tests module at the end of spirc.rs, and a Fixed line next to this one in CHANGELOG.md; I'll rebase whichever merges later.

cargo fmt --all -- --check, cargo clippy --workspace --all-targets -- -D warnings and cargo test --workspace are clean, and there is a changelog entry under Unreleased → Fixed.

🤖 Generated with Claude Code

https://claude.ai/code/session_013zyp13aMbmU4XmKnuCPVRb

When the last track ended with nothing left to play (no repeat, no
autoplay), handle_next stopped the player, which unloaded the track, so a
following play command had nothing to resume until a client started
something new. Spotify's own clients instead go back to the first track of
the context, loaded and paused, so play starts it again.

Rewind to the start of the context and load its first track paused. If the
context cannot be rewound, stop as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 11, 2026 14:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Rewinding at the end of the queue loads the first track. When the end is
reached by skipping a track that failed to load, and every track fails
(the network is down, or the list holds only tracks that can't play),
that load fails as well, `Unavailable` walks the queue to its end again,
and it rewinds again, forever. The `Unavailable` handler now stops there,
as it did before, and only a track that ends or a skip rewinds.

`handle_next` is split: `load_next_track` moves on and loads, returning
whether there was a track, so the end-of-queue policy stays with each
caller. `rewind` holds the state reset `handle_stop` did, so a context
that can't be rewound is reset once and warned about once.

The fixture plays local files that aren't on disk, so the real player
fails to load them without going online, and the new test feeds its
events back until playback stops, failing if it loads more tracks than
the queue holds. The rewind test drops its fixture and refill checks.

The code comment and the changelog no longer claim that play did nothing
after the end: a Connect client's resume reloads the track, and only
`Spirc::play` and `Spirc::play_pause` did nothing. The changelog also
names the changed player events.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013zyp13aMbmU4XmKnuCPVRb
Replace the end-of-queue rewind with a smaller fix. Playback stops at
the end of the queue again, as on dev, and `handle_play` starts the
current track again when playback has stopped, as a Connect client's
resume already did. `Spirc::play` and `Spirc::play_pause`, which an
application's own controls call, then work after the queue ends, after
previous stops at the start of the first track, and after a track that
failed to load stopped the queue.

This keeps the player events at the end of the queue unchanged, loads
nothing until play is pressed, and leaves no failed rewind to handle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013zyp13aMbmU4XmKnuCPVRb
@stefanbd stefanbd changed the title connect: rewind to the start, paused, at the end of the queue connect: let play start again once playback has stopped Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants