A Simple SoundBank Class for Windows Phone 7
When you convert your XBLIGs to Windows Phone 7, you will find yourself without XACT support. For me this meant a ton of "soundBank.PlayCue()" calls that would no longer work. Rather than go through the tedious task of replacing them all with new SoundEffect objects, I created my own SoundBank class that allows me to use the calls exactly as they were but plays then in a WP7 kind of way. This class certainly doesn't encapsulate all the functionality of SoundBank but it does give you enough to play your cues.
In the constructor for the following class you pass in an instance of your ContentManager object. Inside the constructor is where you can load all your sounds (add the wave files to your content project). The constructor adds the sounds to a dictionary object. The first string will be the name you would use in your PlayCue() method in your XBLIG version.
Once you load your sounds, just call PlayCue() the same as you used to. For instance if I wanted to play my AsteroidDestroyed sound, I just call soundBank.PlayCue("AsteroidDestroyed").
public class SoundBank
{
private Dictionary<string, SoundEffect> sounds;
public SoundBank(ContentManager content)
{
sounds = new Dictionary<string, SoundEffect>();
sounds.Add("AsteroidDestroyed", content.Load<SoundEffect>("Audio\\AsteroidDestroyed"));
sounds.Add("Awardment", content.Load<SoundEffect>("Audio\\Awardment"));
}
/// <summary>
/// Play a sound.
/// </summary>
public void PlayCue(string cueName)
{
if (sounds.ContainsKey(cueName))
{
sounds[cueName].Play();
}
}
}
This article has been view 1596 times.
|