initial api impl

This commit is contained in:
2024-10-12 12:15:31 -06:00
parent 666d8b8766
commit 10a13d3daa
12 changed files with 471 additions and 17 deletions
+41
View File
@@ -2,6 +2,10 @@ pub mod info;
use anyhow::Result;
use info::PlaybackInfo;
use librespot::connect::spirc::SpircLoadCommand;
use librespot::core::SpotifyId;
use librespot::metadata::{Metadata, Playlist};
use librespot::protocol::spirc::TrackRef;
use librespot::{
connect::{config::ConnectConfig, spirc::Spirc},
core::{http_client::HttpClientError, Session as SpotifySession, SessionConfig},
@@ -34,6 +38,7 @@ enum PlayerCommand {
GetPlaybackInfo(oneshot::Sender<Option<PlaybackInfo>>),
GetLyrics(oneshot::Sender<Option<Lyrics>>),
QueuePlaylist { uri: String },
Shutdown,
}
@@ -213,6 +218,7 @@ impl Player {
PlayerCommand::GetLyrics(tx) => self.get_lyrics(tx).await,
PlayerCommand::Shutdown => self.commands.close(),
PlayerCommand::QueuePlaylist { uri } => self.queue_playlist(uri).await,
};
}
@@ -300,6 +306,34 @@ impl Player {
_ = tx.send(Some(lyrics));
}
async fn queue_playlist(&self, playlist_uri: String) {
let playlist = Playlist::get(&self.session, &SpotifyId::from_uri(&playlist_uri).unwrap())
.await
.unwrap();
let tracks = playlist
.tracks()
.map(|track_id| {
let mut track = TrackRef::new();
track.set_gid(Vec::from(track_id.to_raw()));
track
})
.collect();
self.spirc.activate().unwrap();
self.spirc
.load(SpircLoadCommand {
context_uri: playlist_uri,
start_playing: true,
shuffle: true,
repeat: true,
playing_track_index: 0,
tracks,
})
.unwrap();
}
}
impl Drop for Player {
@@ -354,4 +388,11 @@ impl PlayerHandle {
pub async fn shutdown(&self) {
_ = self.commands.send(PlayerCommand::Shutdown).await;
}
pub async fn queue_playlist(&self, playlist_uri: String) {
self.commands
.send(PlayerCommand::QueuePlaylist { uri: playlist_uri })
.await
.unwrap()
}
}