How to Play Sounds
Last updated
Last updated
FlatRedBall supports playing sound effects. Sound effects can be loaded from .wav files.
The following will show you how to add a SoundEffect to a Screen. The same method can be used to add a SoundEffect to an Entity as well.
Right-click on a Screen's Files tree node
Add a file to the Screen
If you have an existing .wav file, select Add File->Existing File
If you would like to add a placeholder .wav file, or if you would like to test playing sound effects, select Add File -> New File and select Sound Effect (.wav)
Note: WAV files are used for sound effects. MP3 and WMA files are used for songs. For information on MP3 files, see this file.
Your WAV file should now be part of of the Screen.
To play the sound in code, add the following code to your GameScreen. This assumes that your file is called SoundEffectFile.wav.wav and that you are adding this code to the same Screen/Entity that contains the file:
The SoundEffectFile object in this example is a SoundEffect instance created by Glue. For information on working with the SoundEffect class, see the SoundEffect code reference page. The SoundEffect (which is added to a Screen/Entity when adding a WAV file) can also be played using the AudioManager.
By default the SoundEffect is fire-and-forget, and the only limitation on simultaneous sound effects is from the hardware. However, using the Duration property, a SoundEffect can be limited. In concept, to accomplish this the code needs to keep track of a variable for how many sounds are playing. When a sound plays, increment the number. When a sound stops playing, decrement the number.
To control volume, looping, and panning, you will need to use a SoundEffectInstance object. For information on this, see the SoundEffectInstance Glue reference page.
You may need to convert your file to 8 or 16-bit as XNA PC may have problems with 24 bit audio. For more information, see this page.