Skip to content

Commit

Permalink
map media control Pause and Resume to PlayerRequests (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebRollen authored Oct 20, 2023
1 parent 4c5d25c commit 9ba3a03
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
14 changes: 14 additions & 0 deletions spotify_player/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ impl Client {
match request {
PlayerRequest::NextTrack => self.spotify.next_track(device_id).await?,
PlayerRequest::PreviousTrack => self.spotify.previous_track(device_id).await?,
PlayerRequest::Resume => {
if !playback.is_playing {
self.spotify.resume_playback(device_id, None).await?;
playback.is_playing = true;
state.player.write().buffered_playback = Some(playback);
}
}
PlayerRequest::Pause => {
if playback.is_playing {
self.spotify.pause_playback(device_id).await?;
playback.is_playing = false;
state.player.write().buffered_playback = Some(playback);
}
}
PlayerRequest::ResumePause => {
if !playback.is_playing {
self.spotify.resume_playback(device_id, None).await?
Expand Down
2 changes: 2 additions & 0 deletions spotify_player/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ mod window;
pub enum PlayerRequest {
NextTrack,
PreviousTrack,
Resume,
Pause,
ResumePause,
SeekTrack(chrono::Duration),
Repeat,
Expand Down
12 changes: 11 additions & 1 deletion spotify_player/src/media_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,17 @@ pub fn start_event_watcher(
controls.attach(move |e| {
tracing::info!("Got a media control event: {e:?}");
match e {
MediaControlEvent::Play | MediaControlEvent::Pause | MediaControlEvent::Toggle => {
MediaControlEvent::Play => {
client_pub
.send(ClientRequest::Player(PlayerRequest::Resume))
.unwrap_or_default();
}
MediaControlEvent::Pause => {
client_pub
.send(ClientRequest::Player(PlayerRequest::Pause))
.unwrap_or_default();
}
MediaControlEvent::Toggle => {
client_pub
.send(ClientRequest::Player(PlayerRequest::ResumePause))
.unwrap_or_default();
Expand Down

0 comments on commit 9ba3a03

Please sign in to comment.