Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2222,6 +2222,17 @@ description = "Shows how to directly play a simple pitch"
category = "Audio"
wasm = true

[[example]]
name = "play_on_event"
path = "examples/audio/play_on_event.rs"
doc-scrape-examples = true

[package.metadata.example.play_on_event]
name = "Play on Event"
description = "Shows how to play a sound effect on an event"
category = "Audio"
wasm = true

# Diagnostics
[[example]]
name = "log_diagnostics"
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ Example | Description
[Audio Control](../examples/audio/audio_control.rs) | Shows how to load and play an audio file, and control how it's played
[Decodable](../examples/audio/decodable.rs) | Shows how to create and register a custom audio source by implementing the `Decodable` type.
[Pitch](../examples/audio/pitch.rs) | Shows how to directly play a simple pitch
[Play on Event](../examples/audio/play_on_event.rs) | Shows how to play a sound effect on an event
[Soundtrack](../examples/audio/soundtrack.rs) | Shows how to play different soundtracks based on game state
[Spatial Audio 2D](../examples/audio/spatial_audio_2d.rs) | Shows how to play spatial audio, and moving the emitter in 2D
[Spatial Audio 3D](../examples/audio/spatial_audio_3d.rs) | Shows how to play spatial audio, and moving the emitter in 3D
Expand Down
46 changes: 46 additions & 0 deletions examples/audio/play_on_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//! This example illustrates how to play a sound effect on an event.
//! In this case, we will play a sound effect when the space key is pressed.
use bevy::prelude::*;

#[derive(Resource, Deref)]
struct SoundEffect(Handle<AudioSource>);

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, keyboard_event)
.run();
}

fn setup(asset_server: Res<AssetServer>, mut commands: Commands) {
// Load an asset as a global resource
let handle = asset_server.load("sounds/breakout_collision.ogg");
commands.insert_resource(SoundEffect(handle));

// example instructions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also have to spawn a camera before the instructions will appear

Suggested change
// example instructions
commands.spawn(Camera2d);
// example instructions

commands.spawn(Camera2d);
commands.spawn((
Text::new("Press Space to play the sound effect."),
Node {
position_type: PositionType::Absolute,
bottom: px(12),
left: px(12),
..default()
},
));
}

// spawn an audio player with the sound effect when the space key is pressed
fn keyboard_event(
keyboard_input: Res<ButtonInput<KeyCode>>,
sound_effect: Res<SoundEffect>,
mut commands: Commands,
) {
if keyboard_input.just_pressed(KeyCode::Space) {
commands.spawn((
AudioPlayer::new(sound_effect.clone()),
PlaybackSettings::DESPAWN,
));
}
}