title | description | requireMSLicense |
---|---|---|
How to play a Sound(effect) |
This topic demonstrates how to play a simple sound by using SoundEffect. |
true |
In this example we will take our first steps into audio nirvana and play a simple one-time-only SoundEffect.
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.
-
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.
-
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.
-
Demonstrates how to loop a sound.
-
SoundEffect Class
Provides a loaded sound resource.
-
SoundEffectInstance Class
Provides a single playing, paused, or stopped instance of a SoundEffect sound.