How to play a Sound(effect)
This topic demonstrates how to play a simple sound by using SoundEffect.
Overview
In this example we will take our first steps into audio nirvana and play a simple one-time-only SoundEffect.
Requirements
This sample uses a sound file named tx0_fire1.wav, which you can download from this link here. (save-as) But you can use your own if you wish.
To add a sound file to your project
Open your MonoGame game in your editor of choice.
Open the
Content.mgcb
file by either double-clicking on it, or right-clicking and selecting `Open in MGCB editor". (depending on your editor)Click Add in the menu bar, and then click Existing Item. (alternatively, use the icon for the same task in the editor)
Navigate to the sound file you want to add, and then select it.
The selected audio file is inserted into your project. By default, it is processed by the Content Pipeline, and built wave files are accessed automatically when the game is built.
To play a simple sound
Declare a SoundEffect object to hold the sound file.
// Audio objects SoundEffect soundEffect;
Load the sound file using Content.Load in the 'LoadContent' method.
soundEffect = Content.Load<SoundEffect>("tx0_fire1");
Play the sound.
// Play the sound soundEffect.Play();
Note
Obviously you would not normally call play
in the LoadContent
method, we just use here as an example.
See Also
-
Demonstrates how to loop a sound.
Reference
-
Provides a loaded sound resource.
-
Provides a single playing, paused, or stopped instance of a SoundEffect sound.