The video above I came across is the fourth part of a MonoGame tutorial series. It covers all aspects of audio programming, playing music, sound effects, and even creating audio using XACT from the XNA tools. Below I show how I got sound effects working better recently in XNA. Sound is crucial as it helps to further bring the game to life. In XNA, sound is pretty straightforward. Import the sound into your project, just like you did with textures. WAV is the preferred format in most environment, however MP3 should be fine too. Other than the project knowing the existence of the sound, the program must store it too, so you will need to create a SoundEffect object with a suitable name. I will use the name Gear1SoundEffect as an example based of my vehicle physics program. The command should be:
Gear1SoundEffect = content.Load
Now, to get this to play, you will use a function that is built into the class, conveniently,
This will play the sound effect once and then stop.
Although you can overlap sounds without any problems, if you repeatedly play the same effect at a high rate, the sound will be distorted beyond all recognition. Check out the first time I ran into this problem in an older video of mine here as an example. I needed to limit the rate at which the sound effect was being played using timers; especially since the game program is now running at high frame-rates. So if you have been wondering why most my videos thus far don't have any sound, this is the reason as to why.
To fix this I created an object called SoundEffectInstance, assigning it the returned object from Gear1SoundEffect.CreateInstance(). After that, all I did was call it’s Play() function. This class has many interesting functions that I suggest you check out on MSDN. Other than Pause(), Stop() and Resume(), an additional function is Apply3D(), which gives it a position in 3D space, taking as arguments one or more listeners and an emitter. This will probably not be of use to you currently, but if you move into 3D XNA programming, it will be of great value.
Note that you may have up to 16 different SoundEffectInstance object playing simultaneously, this should be more than enough however. Now you can add some life to your game, essentially anything that does not require rotation of images can now be done with what you know. For more in-depth information about fixing the high rate sound issues, check the blog post from Ely Bob's space on audio in XNA.
For more information, check out this article here.
If you found this blog post helpful, please comment below. Thanks for reading and happy coding!