Skip to content

Commit c0477c9

Browse files
authored
Fix: Use the provided index or the first track as fallback value (#1599)
1 parent ddbdc65 commit c0477c9

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- [player] `preload` function changed from accepting a `SpotifyId` to accepting a `SpotifyUri` (breaking)
2121
- [spclient] `get_radio_for_track` function changed from accepting a `SpotifyId` to accepting a `SpotifyUri` (breaking)
2222

23+
### Fixed
24+
25+
- [connect] Use the provided index or the first as fallback value to always play a track on loading
2326

2427
### Removed
2528

connect/src/spirc.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ impl SpircTask {
651651
SpircCommand::RepeatTrack(repeat) => self.handle_repeat_track(repeat),
652652
SpircCommand::SetPosition(position) => self.handle_seek(position),
653653
SpircCommand::SetVolume(volume) => self.set_volume(volume),
654-
SpircCommand::Load(command) => self.handle_load(command, None).await?,
654+
SpircCommand::Load(command) => self.handle_load(command, None, None).await?,
655655
};
656656

657657
self.notify().await
@@ -998,6 +998,13 @@ impl SpircTask {
998998
.map(Into::into)
999999
.map(LoadContextOptions::Options);
10001000

1001+
let fallback_index = play
1002+
.options
1003+
.skip_to
1004+
.as_ref()
1005+
.and_then(|s| s.track_index)
1006+
.map(|i| i as usize);
1007+
10011008
self.handle_load(
10021009
LoadRequest {
10031010
context,
@@ -1009,6 +1016,7 @@ impl SpircTask {
10091016
},
10101017
},
10111018
play.context.pages.pop(),
1019+
fallback_index,
10121020
)
10131021
.await?;
10141022

@@ -1217,6 +1225,7 @@ impl SpircTask {
12171225
&mut self,
12181226
cmd: LoadRequest,
12191227
page: Option<ContextPage>,
1228+
fallback_index: Option<usize>,
12201229
) -> Result<(), Error> {
12211230
self.connect_state
12221231
.reset_context(if let PlayContext::Uri(ref uri) = cmd.context {
@@ -1253,17 +1262,26 @@ impl SpircTask {
12531262
let index = match cmd_options.playing_track {
12541263
None => None,
12551264
Some(ref playing_track) => Some(match playing_track {
1256-
PlayingTrack::Index(i) => *i as usize,
1265+
PlayingTrack::Index(i) => Ok(*i as usize),
12571266
PlayingTrack::Uri(uri) => {
12581267
let ctx = self.connect_state.get_context(ContextType::Default)?;
1259-
ConnectState::find_index_in_context(ctx, |t| &t.uri == uri)?
1268+
ConnectState::find_index_in_context(ctx, |t| &t.uri == uri)
12601269
}
12611270
PlayingTrack::Uid(uid) => {
12621271
let ctx = self.connect_state.get_context(ContextType::Default)?;
1263-
ConnectState::find_index_in_context(ctx, |t| &t.uid == uid)?
1272+
ConnectState::find_index_in_context(ctx, |t| &t.uid == uid)
12641273
}
12651274
}),
1266-
};
1275+
}
1276+
.map(|i| {
1277+
i.unwrap_or_else(|why| {
1278+
warn!(
1279+
"Failed to resolve index by {:?}, using fallback index: {:?} (Error: {why})",
1280+
cmd_options.playing_track, fallback_index
1281+
);
1282+
fallback_index.unwrap_or_default()
1283+
})
1284+
});
12671285

12681286
if let Some(LoadContextOptions::Options(ref options)) = cmd_options.context_options {
12691287
debug!(

discovery/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ fn launch_libmdns(
406406
}
407407
.map_err(|e| DiscoveryError::DnsSdError(Box::new(e)))?;
408408

409-
let svc = responder.register(&DNS_SD_SERVICE_NAME, &name, port, &TXT_RECORD);
409+
let svc = responder.register(DNS_SD_SERVICE_NAME, &name, port, &TXT_RECORD);
410410

411411
let _ = shutdown_rx.blocking_recv();
412412

0 commit comments

Comments
 (0)