1
0
Fork 0
mirror of https://github.com/librespot-org/librespot.git synced 2025-10-03 01:39:28 +02:00

Prepare for gapless play.

- change communication between player and spirc to use player events channel.
- enhance player events channel
- have spirc send loading messages to Spotify
- enable preloading of tracks in the player
This commit is contained in:
Konstantin Seiler 2020-02-01 08:41:11 +11:00
parent 3672214e31
commit 5784b4652c
6 changed files with 1375 additions and 398 deletions

View file

@ -7,8 +7,53 @@ use librespot::core::session::Session;
use librespot::core::spotify_id::SpotifyId;
use librespot::playback::config::PlayerConfig;
use futures::stream::Stream;
use futures::{Async, Future, Poll};
use librespot::playback::audio_backend;
use librespot::playback::player::Player;
use librespot::playback::player::{Player, PlayerEvent, PlayerEventChannel};
pub struct SingleTrackPlayer {
play_request_id: u64,
event_channel: PlayerEventChannel,
}
impl SingleTrackPlayer {
pub fn new(ref mut player: Player, track_id: SpotifyId) -> SingleTrackPlayer {
let event_channel = player.get_player_event_channel();
let play_request_id = player.load(track_id, true, 0);
SingleTrackPlayer {
play_request_id,
event_channel,
}
}
}
impl Future for SingleTrackPlayer {
type Item = ();
type Error = ();
fn poll(&mut self) -> Poll<(), ()> {
loop {
match self.event_channel.poll().unwrap() {
Async::NotReady => return Ok(Async::NotReady),
Async::Ready(None) => return Ok(Async::Ready(())),
Async::Ready(Some(event)) => match event {
PlayerEvent::EndOfTrack {
play_request_id, ..
}
| PlayerEvent::Stopped {
play_request_id, ..
} => {
if play_request_id == self.play_request_id {
return Ok(Async::Ready(()));
}
}
_ => (),
},
}
}
}
}
fn main() {
let mut core = Core::new().unwrap();
@ -39,7 +84,7 @@ fn main() {
});
println!("Playing...");
core.run(player.load(track, true, 0)).unwrap();
core.run(SingleTrackPlayer::new(player, track)).unwrap();
println!("Done");
}