Skip to content

Add audio/play_on_event example#23219

Open
zerocukor287 wants to merge 2 commits intobevyengine:mainfrom
zerocukor287:main
Open

Add audio/play_on_event example#23219
zerocukor287 wants to merge 2 commits intobevyengine:mainfrom
zerocukor287:main

Conversation

@zerocukor287
Copy link
Contributor

@zerocukor287 zerocukor287 commented Mar 4, 2026

Add audio/play_on_event 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. This example loads a sound effect on startup, and on a keyboard event spawns a 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
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
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
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.

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

Add an example that demonstrate how to load an audio file and play multiple times on a specific event (pressing space).
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-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

3 participants