Skip to content

Commit

Permalink
Merge pull request #9 from GitGhillie/audio-control-example
Browse files Browse the repository at this point in the history
Recreate audio_control example
  • Loading branch information
SolarLiner authored May 5, 2024
2 parents 057ba4c + de68f8b commit 184780c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ version = "0.13.0"
default-features = false
features = ["bevy_asset"]

[dev-dependencies]
bevy = "0.13"

[features]
default = []
diagnostics = []
Binary file added assets/Windless Slopes.ogg
Binary file not shown.
75 changes: 75 additions & 0 deletions examples/audio_control.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//! This example illustrates how to load and play an audio file.
//! For loading additional audio formats, you can enable the corresponding feature for that audio format.
use bevy::prelude::*;
use bevy::utils::error;
use bevy_kira_components::kira::sound::{PlaybackRate, PlaybackState};
use bevy_kira_components::kira::tween::Tween;
use bevy_kira_components::prelude::*;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(AudioPlugin)
.add_systems(Startup, setup)
.add_systems(Update, (update_speed, pause, volume))
.run();
}

fn setup(asset_server: Res<AssetServer>, mut commands: Commands) {
commands.spawn((
AudioFileBundle {
source: asset_server.load("Windless Slopes.ogg"),
..default()
},
MyMusic,
));
}

#[derive(Component)]
struct MyMusic;

fn update_speed(
mut music_controller: Query<&mut AudioHandle<AudioFileHandle>, With<MyMusic>>,
time: Res<Time>,
) {
if let Ok(mut control) = music_controller.get_single_mut() {
let factor = ((time.elapsed_seconds() / 5.0).sin() + 1.0).max(0.1);
error(control.set_playback_rate(PlaybackRate::Factor(factor as f64), Tween::default()));
}
}

fn pause(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut music_controller: Query<&mut AudioHandle<AudioFileHandle>, With<MyMusic>>,
) {
if keyboard_input.just_pressed(KeyCode::Space) {
if let Ok(mut control) = music_controller.get_single_mut() {
match control.playback_state() {
PlaybackState::Playing => {
error(control.pause(Tween::default()));
}
PlaybackState::Pausing | PlaybackState::Paused => {
error(control.resume(Tween::default()));
}
PlaybackState::Stopping | PlaybackState::Stopped => {}
}
}
}
}

fn volume(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut music_controller: Query<&mut AudioHandle<AudioFileHandle>, With<MyMusic>>,
mut target_volume: Local<f64>,
) {
if let Ok(mut control) = music_controller.get_single_mut() {
if keyboard_input.just_pressed(KeyCode::Equal) {
*target_volume += 0.1;
error(control.set_volume(*target_volume + 1.0, Tween::default()));
} else if keyboard_input.just_pressed(KeyCode::Minus) {
*target_volume -= 0.1;
error(control.set_volume(*target_volume + 1.0, Tween::default()));
}
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
//!
//! ## Example
//!
//! ```rust
//! ```no_run
//! use bevy::prelude::*;
//! use bevy_kira_components::prelude::*;
//!
Expand Down

0 comments on commit 184780c

Please sign in to comment.