XNA and Winform with Audio

Unfortunately, I do not know a better way of doing this, so the following is one of the ways. If you know of a better way, you should let me (or the class) know.


Functionality needed: audio file is an external resource that must be loaded-into our program during runtime. XNA provides excellent support in the form of Content Pipeline at compile time and ContentManager and SoundEffect data types at runtime:

All we need to work with Audio is to include the above three items. Unfortunately, the first two items are non-trivial. In short, here is a very slightly modified version of Tutorial 5.6. Unzip this and you will see two .sln files, if you open Tutorial_5_6.sln you will get our existing project and audio does not work. If you open XNA_BallShootWithAudio.sln you will have audio support.


Source code modification:


Project modification: notice the XNA_BallShootWithAudio project is different from your usual C#/Winform project because it includes a Content project. This Content project is the Content Pipeline. In the OS file system, you will notice the corresponding "Content" folder, and included in that folder is the "Bounce.wav" file. This project is responsible for translating the ".wav" file into ".xnb" format (more later). I have no idea how to create and include Content Pipeline project in a C#/Winform project, so I hacked. This is what I did:

At this point I can compile and run my project and when two balls collide, you will hear a nice "bounce" effect.


Run-time environment: Notice the output from "building", you will see "Building Bounce.wav -> ... Bounce.xnb". This is the work performed by the Content Pipeline (the Content) sub-folder's Content.contentproj file. If you go into your file system and find the .exe file (under bin/x86/Debug), you will see the XNA_BallShoot.exe file. Of course, we know by now this is the executable file. But, you will also notice the "Content" subfolder in there that contains the "Bounce.xnb" file. This is the file generated by the Content Pipeline from your Bounce.wav file that was added into the project (last step in the Project Modification). This is the file we actually load at run time.


ks Oct, 2009