diff --git a/src/bin/cli.rs b/src/bin/cli.rs index 74e71b8..45fdc53 100644 --- a/src/bin/cli.rs +++ b/src/bin/cli.rs @@ -75,7 +75,10 @@ enum GetCommands { /// Check if the player is paused IsPaused, /// Playback volume - Volume, + Volume { + #[clap(short, long)] + id: Option, + }, /// Playback position (in seconds) Position { #[clap(short, long)] @@ -146,7 +149,7 @@ async fn main() -> Result<(), Box> { }, Commands::Get { parameter } => match parameter { GetCommands::IsPaused => Request::get_is_paused(), - GetCommands::Volume => Request::get_volume(), + GetCommands::Volume { id } => Request::get_volume(id), GetCommands::Position { id } => Request::get_position(id), GetCommands::Duration { id } => Request::get_duration(id), GetCommands::State => Request::get_state(), diff --git a/src/types/audio_player.rs b/src/types/audio_player.rs index 3d349a6..3484cbe 100644 --- a/src/types/audio_player.rs +++ b/src/types/audio_player.rs @@ -208,6 +208,18 @@ impl AudioPlayer { PlayerState::Stopped } + pub fn get_volume(&mut self, id: Option) -> Option { + if let Some(id) = id { + if let Some(sound) = self.tracks.get_mut(&id) { + return Some(sound.sink.volume()); + } else { + return None; + } + } else { + return Some(self.volume); + } + } + pub fn set_volume(&mut self, volume: f32, id: Option) { if let Some(id) = id { if let Some(sound) = self.tracks.get_mut(&id) { diff --git a/src/types/commands.rs b/src/types/commands.rs index ea0baac..69410c5 100644 --- a/src/types/commands.rs +++ b/src/types/commands.rs @@ -40,7 +40,9 @@ pub struct IsPausedCommand {} pub struct GetStateCommand {} -pub struct GetVolumeCommand {} +pub struct GetVolumeCommand { + pub id: Option, +} pub struct SetVolumeCommand { pub volume: Option, @@ -188,9 +190,14 @@ impl Executable for GetStateCommand { #[async_trait] impl Executable for GetVolumeCommand { async fn execute(&self) -> Response { - let audio_player = get_audio_player().await.lock().await; - let volume = audio_player.volume; + let mut audio_player = get_audio_player().await.lock().await; + let volume = audio_player.get_volume(self.id); + + if let Some(volume) = volume { Response::new(true, volume.to_string()) + } else { + Response::new(false, "Failed to get volume") + } } } diff --git a/src/types/socket.rs b/src/types/socket.rs index 6b69534..263ba5d 100644 --- a/src/types/socket.rs +++ b/src/types/socket.rs @@ -82,8 +82,14 @@ impl Request { Request::new("is_paused", vec![]) } - pub fn get_volume() -> Self { - Request::new("get_volume", vec![]) + pub fn get_volume(id: Option) -> Self { + let mut args = vec![]; + let id_str; + if let Some(id) = id { + id_str = id.to_string(); + args.push(("id", id_str.as_str())); + } + Request::new("get_volume", args) } pub fn get_position(id: Option) -> Self { diff --git a/src/utils/commands.rs b/src/utils/commands.rs index 0a42b8a..ce86e8e 100644 --- a/src/utils/commands.rs +++ b/src/utils/commands.rs @@ -14,7 +14,7 @@ pub fn parse_command(request: &Request) -> Option> { "stop" => Some(Box::new(StopCommand { id })), "is_paused" => Some(Box::new(IsPausedCommand {})), "get_state" => Some(Box::new(GetStateCommand {})), - "get_volume" => Some(Box::new(GetVolumeCommand {})), + "get_volume" => Some(Box::new(GetVolumeCommand { id })), "set_volume" => { let volume = request .args