Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add a way to toggle AudioSink #6321

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion crates/bevy_audio/src/audio_output.rs
Expand Up @@ -178,9 +178,20 @@ impl AudioSink {
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?
///
/// 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 {
self.sink.as_ref().unwrap().is_paused()
}
Expand Down
6 changes: 1 addition & 5 deletions examples/audio/audio_control.rs
Expand Up @@ -43,11 +43,7 @@ fn pause(
) {
if keyboard_input.just_pressed(KeyCode::Space) {
if let Some(sink) = audio_sinks.get(&music_controller.0) {
if sink.is_paused() {
sink.play();
} else {
sink.pause();
}
sink.toggle();
}
}
}
Expand Down