librespot/examples/play.rs

55 lines
1.4 KiB
Rust
Raw Normal View History

2022-01-16 00:29:50 +00:00
use std::{env, process::exit};
use librespot::{
core::{
2022-11-22 12:17:29 +00:00
authentication::Credentials,
config::SessionConfig,
session::Session,
spotify_id::{SpotifyId, SpotifyItemType},
2022-01-16 00:29:50 +00:00
},
playback::{
audio_backend,
config::{AudioFormat, PlayerConfig},
mixer::NoOpVolume,
2022-01-16 00:29:50 +00:00
player::Player,
},
};
2017-04-28 22:24:55 +00:00
#[tokio::main]
async fn main() {
2017-08-03 19:11:30 +00:00
let session_config = SessionConfig::default();
let player_config = PlayerConfig::default();
2021-03-12 22:18:18 +00:00
let audio_format = AudioFormat::default();
2017-04-28 22:24:55 +00:00
2018-02-26 01:50:41 +00:00
let args: Vec<_> = env::args().collect();
if args.len() != 3 {
eprintln!("Usage: {} ACCESS_TOKEN TRACK", args[0]);
return;
2017-04-28 22:24:55 +00:00
}
let credentials = Credentials::with_access_token(&args[1]);
2017-04-28 22:24:55 +00:00
let mut track = SpotifyId::from_base62(&args[2]).unwrap();
2022-11-04 00:10:49 +00:00
track.item_type = SpotifyItemType::Track;
2017-04-28 22:24:55 +00:00
let backend = audio_backend::find(None).unwrap();
2022-01-16 00:29:50 +00:00
println!("Connecting...");
let session = Session::new(session_config, None);
if let Err(e) = session.connect(credentials, false).await {
2022-01-16 00:29:50 +00:00
println!("Error connecting: {}", e);
exit(1);
}
2017-04-28 22:24:55 +00:00
2023-06-13 14:56:40 +00:00
let player = Player::new(player_config, session, Box::new(NoOpVolume), move || {
backend(None, audio_format)
});
2017-04-28 22:24:55 +00:00
2020-02-02 00:07:05 +00:00
player.load(track, true, 0);
2017-04-28 22:24:55 +00:00
println!("Playing...");
player.await_end_of_track().await;
2017-04-28 22:24:55 +00:00
println!("Done");
}