2017-04-28 22:24:55 +00:00
|
|
|
use std::env;
|
|
|
|
use tokio_core::reactor::Core;
|
|
|
|
|
2019-10-07 21:19:45 +00:00
|
|
|
use librespot::core::authentication::Credentials;
|
|
|
|
use librespot::core::config::SessionConfig;
|
|
|
|
use librespot::core::session::Session;
|
|
|
|
use librespot::core::spotify_id::SpotifyId;
|
2018-02-26 01:50:41 +00:00
|
|
|
use librespot::playback::config::PlayerConfig;
|
2017-08-03 19:11:30 +00:00
|
|
|
|
2020-01-31 21:41:11 +00:00
|
|
|
use futures::stream::Stream;
|
|
|
|
use futures::{Async, Future, Poll};
|
2018-02-22 12:24:04 +00:00
|
|
|
use librespot::playback::audio_backend;
|
2020-01-31 21:41:11 +00:00
|
|
|
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(()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-28 22:24:55 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut core = Core::new().unwrap();
|
|
|
|
let handle = core.handle();
|
|
|
|
|
2017-08-03 19:11:30 +00:00
|
|
|
let session_config = SessionConfig::default();
|
|
|
|
let player_config = PlayerConfig::default();
|
2017-04-28 22:24:55 +00:00
|
|
|
|
2018-02-26 01:50:41 +00:00
|
|
|
let args: Vec<_> = env::args().collect();
|
2017-04-28 22:24:55 +00:00
|
|
|
if args.len() != 4 {
|
|
|
|
println!("Usage: {} USERNAME PASSWORD TRACK", args[0]);
|
|
|
|
}
|
|
|
|
let username = args[1].to_owned();
|
|
|
|
let password = args[2].to_owned();
|
|
|
|
let credentials = Credentials::with_password(username, password);
|
|
|
|
|
2018-02-25 02:04:07 +00:00
|
|
|
let track = SpotifyId::from_base62(&args[3]).unwrap();
|
2017-04-28 22:24:55 +00:00
|
|
|
|
|
|
|
let backend = audio_backend::find(None).unwrap();
|
|
|
|
|
|
|
|
println!("Connecting ..");
|
2018-07-03 11:08:42 +00:00
|
|
|
let session = core
|
|
|
|
.run(Session::connect(session_config, credentials, None, handle))
|
2018-02-26 01:50:41 +00:00
|
|
|
.unwrap();
|
2017-04-28 22:24:55 +00:00
|
|
|
|
2019-10-08 09:31:18 +00:00
|
|
|
let (player, _) = Player::new(player_config, session.clone(), None, move || {
|
|
|
|
(backend)(None)
|
|
|
|
});
|
2017-04-28 22:24:55 +00:00
|
|
|
|
|
|
|
println!("Playing...");
|
2020-01-31 21:41:11 +00:00
|
|
|
core.run(SingleTrackPlayer::new(player, track)).unwrap();
|
2017-04-28 22:24:55 +00:00
|
|
|
|
|
|
|
println!("Done");
|
|
|
|
}
|