Added more control fields for playback control
This commit is contained in:
@@ -16,3 +16,4 @@ anyhow = "1.0.86"
|
||||
log = "0.4.22"
|
||||
symphonia = { version = "0.5.4", default-features = false, features = ["pcm"] }
|
||||
hex = "0.4.3"
|
||||
rand = "0.8.5"
|
||||
|
||||
+24
-13
@@ -1,5 +1,7 @@
|
||||
pub mod info;
|
||||
pub mod playback_control;
|
||||
|
||||
use crate::playback_control::{PlayPlaylist, StartAt};
|
||||
use anyhow::Result;
|
||||
use info::PlaybackInfo;
|
||||
use librespot::connect::spirc::SpircLoadCommand;
|
||||
@@ -18,6 +20,7 @@ use librespot::{
|
||||
},
|
||||
};
|
||||
use log::{error, trace};
|
||||
use rand::{thread_rng, Rng};
|
||||
use songbird::{input::RawAdapter, tracks::TrackHandle, Call};
|
||||
use spoticord_audio::{
|
||||
sink::{SinkEvent, StreamSink},
|
||||
@@ -38,7 +41,7 @@ enum PlayerCommand {
|
||||
|
||||
GetPlaybackInfo(oneshot::Sender<Option<PlaybackInfo>>),
|
||||
GetLyrics(oneshot::Sender<Option<Lyrics>>),
|
||||
QueuePlaylist { uri: String },
|
||||
QueuePlaylist(PlayPlaylist),
|
||||
|
||||
Shutdown,
|
||||
}
|
||||
@@ -218,7 +221,7 @@ impl Player {
|
||||
PlayerCommand::GetLyrics(tx) => self.get_lyrics(tx).await,
|
||||
|
||||
PlayerCommand::Shutdown => self.commands.close(),
|
||||
PlayerCommand::QueuePlaylist { uri } => self.queue_playlist(uri).await,
|
||||
PlayerCommand::QueuePlaylist(play_playlist) => self.queue_playlist(play_playlist).await,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -307,12 +310,15 @@ 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();
|
||||
async fn queue_playlist(&self, playlist_control: PlayPlaylist) {
|
||||
let playlist = Playlist::get(
|
||||
&self.session,
|
||||
&SpotifyId::from_uri(&playlist_control.uri).unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let tracks = playlist
|
||||
let tracks: Vec<_> = playlist
|
||||
.tracks()
|
||||
.map(|track_id| {
|
||||
let mut track = TrackRef::new();
|
||||
@@ -323,13 +329,18 @@ impl Player {
|
||||
|
||||
self.spirc.activate().unwrap();
|
||||
|
||||
let start_ndx = match playlist_control.start_at {
|
||||
StartAt::FirstSong => 0u32,
|
||||
StartAt::Random => thread_rng().gen_range(0..tracks.len()) as u32,
|
||||
};
|
||||
|
||||
self.spirc
|
||||
.load(SpircLoadCommand {
|
||||
context_uri: playlist_uri,
|
||||
context_uri: playlist_control.uri,
|
||||
start_playing: true,
|
||||
shuffle: true,
|
||||
repeat: true,
|
||||
playing_track_index: 0,
|
||||
shuffle: playlist_control.shuffle,
|
||||
repeat: playlist_control.repeat,
|
||||
playing_track_index: start_ndx,
|
||||
tracks,
|
||||
})
|
||||
.unwrap();
|
||||
@@ -389,9 +400,9 @@ impl PlayerHandle {
|
||||
_ = self.commands.send(PlayerCommand::Shutdown).await;
|
||||
}
|
||||
|
||||
pub async fn queue_playlist(&self, playlist_uri: String) {
|
||||
pub async fn queue_playlist(&self, play_playlist: PlayPlaylist) {
|
||||
self.commands
|
||||
.send(PlayerCommand::QueuePlaylist { uri: playlist_uri })
|
||||
.send(PlayerCommand::QueuePlaylist(play_playlist))
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum StartAt {
|
||||
FirstSong,
|
||||
Random,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PlayPlaylist {
|
||||
pub uri: String,
|
||||
pub shuffle: bool,
|
||||
pub repeat: bool,
|
||||
pub start_at: StartAt,
|
||||
}
|
||||
Reference in New Issue
Block a user