play.rs 1.3 KB

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