play.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. extern crate librespot;
  2. extern crate tokio_core;
  3. use std::env;
  4. use tokio_core::reactor::Core;
  5. use librespot::audio_backend;
  6. use librespot::authentication::Credentials;
  7. use librespot::player::Player;
  8. use librespot::session::{Config, Session};
  9. use librespot::util::SpotifyId;
  10. fn main() {
  11. let mut core = Core::new().unwrap();
  12. let handle = core.handle();
  13. let config = Config::default();
  14. let args : Vec<_> = env::args().collect();
  15. if args.len() != 4 {
  16. println!("Usage: {} USERNAME PASSWORD TRACK", args[0]);
  17. }
  18. let username = args[1].to_owned();
  19. let password = args[2].to_owned();
  20. let credentials = Credentials::with_password(username, password);
  21. let track = SpotifyId::from_base62(&args[3]);
  22. let backend = audio_backend::find(None).unwrap();
  23. println!("Connecting ..");
  24. let session = core.run(Session::connect(config, credentials, None, handle)).unwrap();
  25. let player = Player::new(session.clone(), None, move || (backend)(None));
  26. println!("Playing...");
  27. core.run(player.load(track, true, 0)).unwrap();
  28. println!("Done");
  29. }