librespot/examples/play.rs

44 lines
1.2 KiB
Rust
Raw Normal View History

2017-04-28 22:24:55 +00:00
extern crate librespot;
extern crate tokio_core;
use std::env;
use tokio_core::reactor::Core;
2017-08-03 19:11:30 +00:00
use librespot::core::authentication::Credentials;
use librespot::playback::config::{PlayerConfig, SessionConfig};
2017-08-03 19:11:30 +00:00
use librespot::core::session::Session;
2018-02-12 20:02:27 +00:00
use librespot::core::spotify_id::SpotifyId;
2017-08-03 19:11:30 +00:00
2017-04-28 22:24:55 +00:00
use librespot::audio_backend;
use librespot::player::Player;
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
let args : Vec<_> = env::args().collect();
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);
let track = SpotifyId::from_base62(&args[3]);
let backend = audio_backend::find(None).unwrap();
println!("Connecting ..");
2017-08-03 19:11:30 +00:00
let session = core.run(Session::connect(session_config, credentials, None, handle)).unwrap();
2017-04-28 22:24:55 +00:00
2017-08-03 19:11:30 +00:00
let player = Player::new(player_config, session.clone(), None, move || (backend)(None));
2017-04-28 22:24:55 +00:00
println!("Playing...");
core.run(player.load(track, true, 0)).unwrap();
println!("Done");
}