Implement track preloading

Will preload the next track close to the end of the currently playing
track. Should make playback of queued tracks a little smoother.
This commit is contained in:
Henrik Friedrichsen
2021-04-11 15:51:19 +02:00
parent decf7c2aef
commit 98e572169b
5 changed files with 38 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
use crate::events::{Event, EventManager};
use crate::playable::Playable;
use crate::queue::QueueEvent;
use crate::spotify::{PlayerEvent, Spotify};
use futures::channel::{mpsc, oneshot};
use futures::compat::Stream01CompatExt;
@@ -25,6 +26,7 @@ pub(crate) enum WorkerCommand {
Seek(u32),
SetVolume(u16),
RequestToken(oneshot::Sender<Token>),
Preload(Playable),
Shutdown,
}
@@ -126,6 +128,12 @@ impl futures::Future for Worker {
self.token_task = Spotify::get_token(&self.session, sender);
progress = true;
}
WorkerCommand::Preload(playable) => {
if let Ok(id) = SpotifyId::from_uri(&playable.uri()) {
debug!("Preloading {:?}", id);
self.player.preload(id);
}
}
WorkerCommand::Shutdown => {
self.player.stop();
self.session.shutdown();
@@ -173,6 +181,10 @@ impl futures::Future for Worker {
self.events.send(Event::Player(PlayerEvent::FinishedTrack));
progress = true;
}
LibrespotPlayerEvent::TimeToPreloadNextTrack { .. } => {
self.events
.send(Event::Queue(QueueEvent::PreloadTrackRequest));
}
_ => {}
}
}