Add a way to toggle AudioSink
(#6321)
# Objective Currently toggling an `AudioSink` (for example from a game menu) requires writing ```rs if sink.is_paused() { sink.play(); } else { sink.pause(); } ``` It would be nicer if we could reduce this down to a single line ```rs sink.toggle(); ``` ## Solution Add an `AudioSink::toggle` method which does exactly that. --- ## Changelog - Added `AudioSink::toggle` which can be used to toggle state of a sink.
This commit is contained in:
parent
13da481bea
commit
599ca782e3
@ -178,9 +178,20 @@ impl AudioSink {
|
|||||||
self.sink.as_ref().unwrap().pause();
|
self.sink.as_ref().unwrap().pause();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Toggles the playback of this sink.
|
||||||
|
///
|
||||||
|
/// Will pause if playing, and will be resumed if paused.
|
||||||
|
pub fn toggle(&self) {
|
||||||
|
if self.is_paused() {
|
||||||
|
self.play();
|
||||||
|
} else {
|
||||||
|
self.pause();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Is this sink paused?
|
/// Is this sink paused?
|
||||||
///
|
///
|
||||||
/// Sinks can be paused and resumed using [`pause`](Self::pause) and [`play`](Self::play).
|
/// Sinks can be paused and resumed using [`pause`](Self::pause), [`play`](Self::play), and [`toggle`](Self::toggle).
|
||||||
pub fn is_paused(&self) -> bool {
|
pub fn is_paused(&self) -> bool {
|
||||||
self.sink.as_ref().unwrap().is_paused()
|
self.sink.as_ref().unwrap().is_paused()
|
||||||
}
|
}
|
||||||
|
@ -43,11 +43,7 @@ fn pause(
|
|||||||
) {
|
) {
|
||||||
if keyboard_input.just_pressed(KeyCode::Space) {
|
if keyboard_input.just_pressed(KeyCode::Space) {
|
||||||
if let Some(sink) = audio_sinks.get(&music_controller.0) {
|
if let Some(sink) = audio_sinks.get(&music_controller.0) {
|
||||||
if sink.is_paused() {
|
sink.toggle();
|
||||||
sink.play();
|
|
||||||
} else {
|
|
||||||
sink.pause();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user