[GH-ISSUE #1493] Get current playback position #673

Closed
opened 2026-02-27 19:31:53 +03:00 by kerem · 5 comments
Owner

Originally created by @fragsalat on GitHub (Apr 28, 2025).
Original GitHub issue: https://github.com/librespot-org/librespot/issues/1493

Hey there, first of all thanks for your great work :)

Is your feature request related to a problem? Please describe.
I would find it helpful, if the Player struct gets a function to retrieve the current playback position in ms. As of now we can control the playback but don't get information about the progress. As the timing for a progress bar using a separate thread with a tokio interval likely diverge from the actual playback progress, it would be helpful to have a way to retrieve the current playback position to correct such a progress bar timer.

Describe the solution you'd like

let player = Player::new(player_config, session, volume.clone(), move || {
    backend(None, audio_format)
});
player.load(self.get_play_id(track)?, true, 0);
// ... somewhere in a progress bar update loop
let position_ms: u32 = player.get_playback_position()

Describe alternatives you've considered
It would be also sufficient if the PlayerConfig could allow a config like notify_playback_progress and in the player.rs:1334 the progress is constantly notified.

Initially I did try to poll the spotify API but it seem like librespot downloads the track and sends the data to the local sink without reporting to spotify the playback status. At least when using the player.load() method.

Additional context
If wanted I could give it a try to implement this.

Originally created by @fragsalat on GitHub (Apr 28, 2025). Original GitHub issue: https://github.com/librespot-org/librespot/issues/1493 Hey there, first of all thanks for your great work :) **Is your feature request related to a problem? Please describe.** I would find it helpful, if the Player struct gets a function to retrieve the current playback position in ms. As of now we can control the playback but don't get information about the progress. As the timing for a progress bar using a separate thread with a tokio interval likely diverge from the actual playback progress, it would be helpful to have a way to retrieve the current playback position to correct such a progress bar timer. **Describe the solution you'd like** ``` let player = Player::new(player_config, session, volume.clone(), move || { backend(None, audio_format) }); player.load(self.get_play_id(track)?, true, 0); // ... somewhere in a progress bar update loop let position_ms: u32 = player.get_playback_position() ``` **Describe alternatives you've considered** It would be also sufficient if the `PlayerConfig` could allow a config like `notify_playback_progress` and in the [player.rs:1334](https://github.com/librespot-org/librespot/blob/dev/playback/src/player.rs#L1334) the progress is constantly notified. Initially I did try to poll the spotify API but it seem like librespot downloads the track and sends the data to the local sink without reporting to spotify the playback status. At least when using the player.load() method. **Additional context** If wanted I could give it a try to implement this.
kerem 2026-02-27 19:31:53 +03:00
Author
Owner

@photovoltex commented on GitHub (Apr 28, 2025):

I think what you describe already exists here as a channel that sends the events that happen with the player. The same channel can also be retrieved when using the Spirc :)
https://github.com/librespot-org/librespot/blob/dev/playback%2Fsrc%2Fplayer.rs#L547

<!-- gh-comment-id:2835310059 --> @photovoltex commented on GitHub (Apr 28, 2025): I think what you describe already exists here as a channel that sends the events that happen with the player. The same channel can also be retrieved when using the `Spirc` :) https://github.com/librespot-org/librespot/blob/dev/playback%2Fsrc%2Fplayer.rs#L547
Author
Owner

@fragsalat commented on GitHub (Apr 28, 2025):

Hey, thanks for the fast respone. I did had a look at the PlayerEvent enum and did not find a correct event which is sent for the progress. There is the ProgressCorrection event but this is only sent under certain circumstances [1] like when seeking.
I would like to get the progress constantly or ad-hoc, depending on whats easier to implement. I think a flag to change the behavior and constantly send the PositionCorrection event would be the simplest solution but not backward compatible as this changes the behavior of the event. So a new event in combination with a flag (if we don't want to always send this due to performance constraints or what ever reasons) could solve this.

[1] https://github.com/librespot-org/librespot/blob/dev/playback/src/player.rs#L1300-L1332

<!-- gh-comment-id:2835345534 --> @fragsalat commented on GitHub (Apr 28, 2025): Hey, thanks for the fast respone. I did had a look at the PlayerEvent enum and did not find a correct event which is sent for the progress. There is the ProgressCorrection event but this is only sent under certain circumstances [1] like when seeking. I would like to get the progress constantly or ad-hoc, depending on whats easier to implement. I think a flag to change the behavior and constantly send the PositionCorrection event would be the simplest solution but not backward compatible as this changes the behavior of the event. So a new event in combination with a flag (if we don't want to always send this due to performance constraints or what ever reasons) could solve this. [1] https://github.com/librespot-org/librespot/blob/dev/playback/src/player.rs#L1300-L1332
Author
Owner

@photovoltex commented on GitHub (Apr 28, 2025):

In addition to the ProgressCorrection the position is also send when the playback state changes as the position_ms field. See:

<!-- gh-comment-id:2836702384 --> @photovoltex commented on GitHub (Apr 28, 2025): In addition to the `ProgressCorrection` the position is also send when the playback state changes as the `position_ms` field. See: - [`PlayerEvent::Loading`](https://github.com/librespot-org/librespot/blob/dev/playback/src/player.rs#L145) - [`PlayerEvent::Playing`](https://github.com/librespot-org/librespot/blob/dev/playback/src/player.rs#L161) - [`PlayerEvent::Paused`](https://github.com/librespot-org/librespot/blob/dev/playback/src/player.rs#L167) - [`PlayerEvent::Seeked`](https://github.com/librespot-org/librespot/blob/dev/playback%2Fsrc%2Fplayer.rs#L198)
Author
Owner

@fragsalat commented on GitHub (Apr 28, 2025):

I did create a PR to extend this with a PositionChanged event, which is emitted every 250ms during playback.
By consuming this it is possible to implement a progress bar during playback.
I did not reuse ProgressCorrection as this would not be backward compatible as mentioned before.

<!-- gh-comment-id:2836792633 --> @fragsalat commented on GitHub (Apr 28, 2025): I did create a PR to extend this with a PositionChanged event, which is emitted every 250ms during playback. By consuming this it is possible to implement a progress bar during playback. I did not reuse ProgressCorrection as this would not be backward compatible as mentioned before.
Author
Owner

@photovoltex commented on GitHub (May 4, 2025):

Huh, the PR didn't seem to have closed the issue. Well whatever. Closing due to #1495 being merged :)

<!-- gh-comment-id:2849353874 --> @photovoltex commented on GitHub (May 4, 2025): Huh, the PR didn't seem to have closed the issue. Well whatever. Closing due to #1495 being merged :)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/librespot#673
No description provided.