play.rs 1.3 KB

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