When dealing with audio in XNA, Xact can be very frustrating so I've decided to write up a VERY short audio tutorial for Windows only. It can handle any audio file that Windows Media Player (WMP) can handle. Such file types are WMA, WAV, MP3, AVI, MIDI, AU, AIFF, and ASF. Why Windows Media Player you may be asking. Well that is what we will be using to play our audio files. I chose this because most people have it installed and it supports quite a few file types. And the best part is it takes only two lines of code to initialize. And one to play the file.
First start a new XNA project and go over to your left on the solution explorer and open up the references tab. Then right click the reference tab and click add reference. Then click and the COM tab. Scroll down to the bottom and click on Windows Media Player. Then press OK. On the right under the references tab you should see WMPLib. Then under the Content Folder add a new one to hold audio files. Mine will simply be called "Audio". Then right click that folder and click Add Existing Item. Then find whatever audio file that fits one of the formats above. I chose an mp3 called Mortal Combat Techno theme. (Odd I know but it makes pumping background music in a game.) Next click on it and go to the properties window. ( View Properties Window) Then change build action to None, and copy to output directory as copy if newer.
Now for the coding. That first section set up the project now to make it happen. Add the following line of code right above the game1 method.
//Our class refferal
WMPLib.WindowsMediaPlayerClass Sound_Player;
Now in the initialize method add these lines of code.
//create an instance of the class
Sound_Player = new WMPLib.WindowsMediaPlayerClass();
//Play the File Make sure to replace the URL with whatever your file is
Sound_Player.IWMPPlayer_URL = "Content/Audio/Mortal Combat Techno Theme.mp3";
And that is it if you did everything correctly your audio file should be playing. Also Inside the WindowsMediaPlayerClass you will find MANY more advanced options for audio. And as always Happy Coding!
Saturday, January 5, 2008
Subscribe to:
Post Comments (Atom)
5 comments:
can it be use to dynamically load music in a game on xna???
Yes, It can
I think the reason for the hassle when we working with XACT is maybe the matter of memory, smaller size and faster-running, maybe. When you work with window media player COM, you may feel how easy it is but let's try to compare the program's size in runtime (i mean the RAM memory it takes)and the size of deployment program. Please tell me the result, I also really wanna know ;)
Post a Comment