XNA: Adding an MP3 Sound Effect
June 19, 2012Following these few steps and lines of code, you learn how to add an mp3 sound effect to your XNA game and replay it, when the user clicks the left mouse button.
1.) In your XNA application simply drop the mp3 file into the Content project.
2.) Make sure you set the mp3 file’s Content Processor property to “Sound Effect – XNA Framework”.
3.) Add a member variable of type SoundEffect to your game class.
private SoundEffect _soundEffect;
4.) In your LoadContent method add this line of code:
_soundEffect = Content.Load<SoundEffect>("hadouken");
5.) To play the sound effect e.g. on a left mouse click, add these lines of code inside your Update method.
var ms = Mouse.GetState(); if (ms.LeftButton == ButtonState.Pressed) { _soundEffect.Play(); }