-
Notifications
You must be signed in to change notification settings - Fork 653
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update connect example and link in docs
- Loading branch information
1 parent
7dc4bf0
commit 8fac548
Showing
3 changed files
with
56 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ spotify_appkey.key | |
.vagrant/ | ||
.project | ||
.history | ||
.cache | ||
*.save | ||
*.*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,74 @@ | ||
use librespot::{ | ||
connect::{ConnectConfig, LoadRequest, LoadRequestOptions, Spirc}, | ||
core::{ | ||
authentication::Credentials, config::SessionConfig, session::Session, spotify_id::SpotifyId, | ||
authentication::Credentials, cache::Cache, config::SessionConfig, session::Session, Error, | ||
}, | ||
metadata::{Album, Metadata}, | ||
playback::mixer::{softmixer::SoftMixer, Mixer, MixerConfig}, | ||
playback::mixer::MixerConfig, | ||
playback::{ | ||
audio_backend, | ||
config::{AudioFormat, PlayerConfig}, | ||
mixer::NoOpVolume, | ||
mixer, | ||
player::Player, | ||
}, | ||
}; | ||
|
||
use std::{env, sync::Arc}; | ||
use tokio::join; | ||
use log::LevelFilter; | ||
|
||
const CACHE: &str = ".cache"; | ||
const CACHE_FILES: &str = ".cache/files"; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
async fn main() -> Result<(), Error> { | ||
env_logger::builder() | ||
.filter_module("librespot", LevelFilter::Debug) | ||
.init(); | ||
|
||
let session_config = SessionConfig::default(); | ||
let player_config = PlayerConfig::default(); | ||
let audio_format = AudioFormat::default(); | ||
let connect_config = ConnectConfig::default(); | ||
let mixer_config = MixerConfig::default(); | ||
let request_options = LoadRequestOptions::default(); | ||
|
||
let mut args: Vec<_> = env::args().collect(); | ||
let context_uri = if args.len() == 3 { | ||
args.pop().unwrap() | ||
} else if args.len() == 2 { | ||
String::from("spotify:album:79dL7FLiJFOO0EoehUHQBv") | ||
} else { | ||
eprintln!("Usage: {} ACCESS_TOKEN (ALBUM URI)", args[0]); | ||
return; | ||
}; | ||
let sink_builder = audio_backend::find(None).unwrap(); | ||
let mixer_builder = mixer::find(None).unwrap(); | ||
|
||
let credentials = Credentials::with_access_token(&args[1]); | ||
let backend = audio_backend::find(None).unwrap(); | ||
let cache = Cache::new(Some(CACHE), Some(CACHE), Some(CACHE_FILES), None)?; | ||
let credentials = cache | ||
.credentials() | ||
.ok_or(Error::unavailable("credentials not cached")) | ||
.or_else(|_| { | ||
librespot_oauth::get_access_token( | ||
&session_config.client_id, | ||
"http://127.0.0.1:8898/login", | ||
vec!["streaming"], | ||
) | ||
.map(|t| Credentials::with_access_token(t.access_token)) | ||
})?; | ||
|
||
println!("Connecting..."); | ||
let session = Session::new(session_config, None); | ||
let session = Session::new(session_config, Some(cache)); | ||
let mixer = mixer_builder(mixer_config); | ||
|
||
let player = Player::new( | ||
player_config, | ||
session.clone(), | ||
Box::new(NoOpVolume), | ||
move || backend(None, audio_format), | ||
mixer.get_soft_volume(), | ||
move || sink_builder(None, audio_format), | ||
); | ||
|
||
let (spirc, spirc_task) = Spirc::new( | ||
connect_config, | ||
session.clone(), | ||
credentials, | ||
player, | ||
Arc::new(SoftMixer::open(MixerConfig::default())), | ||
) | ||
.await | ||
.unwrap(); | ||
let (spirc, spirc_task) = | ||
Spirc::new(connect_config, session.clone(), credentials, player, mixer).await?; | ||
|
||
join!(spirc_task, async { | ||
let album = Album::get(&session, &SpotifyId::from_uri(&context_uri).unwrap()) | ||
.await | ||
.unwrap(); | ||
// these calls can be seen as "queued" | ||
spirc.activate()?; | ||
spirc.load(LoadRequest::from_context_uri( | ||
format!("spotify:user:{}:collection", session.username()), | ||
request_options, | ||
))?; | ||
spirc.play()?; | ||
|
||
println!( | ||
"Playing album: {} by {}", | ||
&album.name, | ||
album | ||
.artists | ||
.first() | ||
.map_or("unknown", |artist| &artist.name) | ||
); | ||
// starting the connect device and processing the previously "queued" calls | ||
spirc_task.await; | ||
|
||
spirc.activate().unwrap(); | ||
spirc | ||
.load(LoadRequest::from_context_uri( | ||
context_uri, | ||
LoadRequestOptions { | ||
start_playing: true, | ||
..Default::default() | ||
}, | ||
)) | ||
.unwrap(); | ||
}); | ||
Ok(()) | ||
} |