feat: add play and pause messages (#24)

This commit is contained in:
danielwerg 2024-10-12 11:54:32 +03:00 committed by GitHub
parent fd8ecfcd05
commit 8d9d003dc9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 4 deletions

View File

@ -48,6 +48,12 @@ pub enum Messages {
/// Similar to Next, but specific to the first track.
Init,
/// Unpause the [Sink].
Play,
/// Pauses the [Sink].
Pause,
/// Pauses the [Sink]. This will also unpause it if it is paused.
PlayPause,
@ -305,6 +311,12 @@ impl Player {
// as to not block the main audio thread.
task::spawn(Self::handle_next(player.clone(), itx.clone(), tx.clone()));
}
Messages::Play => {
player.sink.play();
}
Messages::Pause => {
player.sink.pause();
}
Messages::PlayPause => {
if player.sink.is_paused() {
player.sink.play();

View File

@ -78,7 +78,7 @@ impl PlayerInterface for Player {
async fn pause(&self) -> fdo::Result<()> {
self.sender
.send(Messages::PlayPause)
.send(Messages::Pause)
.await
.map_err(|_| ERROR)
}
@ -91,11 +91,14 @@ impl PlayerInterface for Player {
}
async fn stop(&self) -> fdo::Result<()> {
self.play_pause().await
self.pause().await
}
async fn play(&self) -> fdo::Result<()> {
self.play_pause().await
self.sender
.send(Messages::Play)
.await
.map_err(|_| ERROR)
}
async fn seek(&self, _offset: Time) -> fdo::Result<()> {

View File

@ -88,7 +88,7 @@ async fn input(sender: Sender<Messages>) -> eyre::Result<()> {
event::MediaKeyCode::Play => Messages::PlayPause,
event::MediaKeyCode::Pause => Messages::PlayPause,
event::MediaKeyCode::PlayPause => Messages::PlayPause,
event::MediaKeyCode::Stop => Messages::PlayPause,
event::MediaKeyCode::Stop => Messages::Pause,
event::MediaKeyCode::TrackNext => Messages::Next,
event::MediaKeyCode::LowerVolume => Messages::ChangeVolume(-0.1),
event::MediaKeyCode::RaiseVolume => Messages::ChangeVolume(0.1),