Skip to content

Add audio/play_sound_effect example#23219

Merged
alice-i-cecile merged 5 commits intobevyengine:mainfrom
zerocukor287:main
Mar 29, 2026
Merged

Add audio/play_sound_effect example#23219
alice-i-cecile merged 5 commits intobevyengine:mainfrom
zerocukor287:main

Conversation

@zerocukor287
Copy link
Copy Markdown
Contributor

@zerocukor287 zerocukor287 commented Mar 4, 2026

Add audio/play_sound_effect example

Objective

Add an example that demonstrates how to load an audio file and play it multiple times on a specific event (pressing space).

Solution

It is similar to the breakout example, but way shorter and focused. This example loads a sound effect as a resource with the FromWorld trait, and on a keyboard event spawns an AudioPlayer with DESPAWN mode.

Testing

  • Built on top of 0.18.1, and moved to master
  • Tested by building the examples, and pressing the space a few times (no overlapping audio)
  • Tested also by pressing space as fast as I could do for about 1 minute. Lot of components are spawned, played at the same time, but I've found no issues.

@fallible-algebra
Copy link
Copy Markdown
Contributor

This is a nice basic example, it might make sense to also have the minimum amount of setup needed to have text on screen that says "press space to play audio."

@zerocukor287
Copy link
Copy Markdown
Contributor Author

@fallible-algebra I've added a short text to press space. I think the PR is done from my side.

@kfc35 kfc35 added C-Examples An addition or correction to our examples A-Audio Sounds playback and modification D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Mar 5, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in Audio Mar 5, 2026
Copy link
Copy Markdown
Contributor

@kfc35 kfc35 left a comment

Choose a reason for hiding this comment

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

Tested the example myself and it works.

Approving, as long as spawning a camera is also included.

Comment thread examples/audio/play_on_event.rs Outdated
Add an example that demonstrate how to load an audio file and play multiple times on a specific event (pressing space).
@zerocukor287 zerocukor287 requested a review from kfc35 March 8, 2026 11:05
Comment thread Cargo.toml Outdated
Comment thread Cargo.toml Outdated
Comment thread examples/audio/play_on_event.rs Outdated
Comment thread examples/audio/play_on_event.rs Outdated
Comment thread examples/audio/play_on_event.rs Outdated
Comment thread examples/audio/play_on_event.rs Outdated
@zerocukor287 zerocukor287 changed the title Add audio/play_on_event example Add audio/play_sound_effect example Mar 29, 2026
Comment thread examples/audio/play_sound_effect.rs Outdated
@alice-i-cecile alice-i-cecile removed the S-Needs-Review Needs reviewer attention (from anyone!) to move forward label Mar 29, 2026
@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Mar 29, 2026
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Mar 29, 2026
Merged via the queue into bevyengine:main with commit 665d145 Mar 29, 2026
44 checks passed
@github-project-automation github-project-automation Bot moved this from Needs SME Triage to Done in Audio Mar 29, 2026
splo pushed a commit to splo/bevy that referenced this pull request Mar 31, 2026
Add `audio/play_sound_effect` example

# Objective

Add an example that demonstrates how to load an audio file and play it
multiple times on a specific event (pressing space).

## Solution

It is similar to the breakout example, but way shorter and focused. This
example loads a sound effect as a resource with the `FromWorld` trait,
and on a keyboard event spawns an `AudioPlayer` with `DESPAWN` mode.

## Testing

- Built on top of 0.18.1, and moved to master
- Tested by building the examples, and pressing the space a few times
(no overlapping audio)
- Tested also by pressing space as fast as I could do for about 1
minute. Lot of components are spawned, played at the same time, but I've
found no issues.

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Audio Sounds playback and modification C-Examples An addition or correction to our examples D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants