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:
- Content Pipeline: At compile time, the content pipeline helps
translate the format from external (e.g., .wav file) into something XNA recognizes (i.e., xnb format).
- ContentManager: At run time, ContentManager helps load the
intermediate data file so we can use it in our program.
- SoundEffect: At run time, SoundEffect abstracts an audio device
so that we can "play" our audio effect.
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:
-
UWBGL_XNA_Lib3:
- new class: UWB_XNAAudioSupport.cs: That a look
at this, the implementation is actually quite trivial, the only important
instance variable is mAudioResource, and the only important method is
PlaySound().
- Modified: UWB_XNAGraphicsSystem.cs: to include an
instance of XNAAudioSupport object, and include a "global accessible
function" PlayAudio() at the very bottom of the file.
-
XNA_BallShootWithAudio: line 169 when we detect two balls collided,
the "global function PlayAudio()" is called to play the "Bounce.wav"
sound fragment. Notice, we do not need to include the ".wav"
extension.
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:
- Create a new XNA Windows Game project: off the project wizard. I named
this project "XNA_BallShootWithAudio".
- Removed the unnecessary files: I removed the Game.cs and
Program.cs files from my new XNA_BallShootWithAudio project.
- Copy source files over from Tutorial_5_6 project: I copied all the
source files over to the new BallShootWithAudio folder.
- Include source files from Tutorial_5_6: I Right-MouseButton-Click add
"Existing Item" to include all the source files (e.g., Program.cs,
Form1.cs, etc.) and folders (Model, WindowHandler) into my new project.
- Include our WinForm (UWBGL_WindForms) and UWBGL (UWBGL_XNA_Lib3)
libraries by: Right-Mouse-Click over the solution and adding existing
projects.
- Add necessary References: In the BallShootWithAudio project, under
"References", I added:
- From .net: System.Data, System.Drawing, SystemWindows.Forms
- From Browse: UWBGL_WinForms_Lib1, and UWBGL_XNA_Lib3
- Add the "wav" file into the "Content" project: by
Right-mouse-button-click over the "Content" project and browse to select "Bounce.wav"
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