Introduction
This is part 2 of a multi-part blog post. For Part 1, see this. The first part of this post covered an introduction to the platform and rendering 2D sprites. Here I continue with sound. In the next post, I'll introduce 3D rendering in XNA.
Adding Sound
The sounds that you would need to play in your game in general could be classified as background sounds (such as background music) or incidental sounds (such as sound effects from some event happening). Let's start off making a program that will play a sound using the simplest way available. Create a new XNA Windows Phone Game project. After the project is created, right-click on the Content project and select "Add Existing." Navigate to a small PCM *.wav file on your system and select it to be added to your project. If you don't have any PCM WAV files laying around, I'd suggest downloading a free audio editor such as Audacity and use it to convert a section of a music file to a sound. Once the file is added to your content project, rename it to "MySound.wav". You can rename it by right-clicking on the file and selecting the "rename" option. Within the Update()
method, we are going to add code so that when the user presses and releases any area of the screen, the sound will play. Create a new boolean field for the class called _screenPressed
and a new field of type SoundEffect
named mySoundEffect
. Within the LoadContent()
method,populate mySoundEffect
using Content.Load<SoundEffect>("MySound.wav");
. Now if you run the program, it will play your sound every time you touch the screen.
protected override void Update(GameTime gameTime)
{
var touchState = TouchPanel.GetState();
bool touchDetected = touchState.Count > 0;
if ((!_screenPressed) && (touchDetected))
{
mySoundEffect.Play();
}
_screenPressed = touchDetected;
base.Update(gameTime);
}
If you want to be able to do other things with the sound, you will need to use the SoundEffectInstance
class. A new SoundEffectInstance
can be instantiated with a call to the CreateInstance()
member of the SoundEffect
class. Once you have a SoundEffectInstance
, you can do things such as pause the sound after beginning play, changing the speed at which it plays, or loop the sound. Let's change the program so that it loops the sound as long as the screen is being touched. ADd a new SoundEffectInstance
field named soundEffectLoop
. In the LoadContent();
method, right after mySoundEffect
is populated use the CreateInstance()
method to populate soundEffectLoop
.
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
mySoundEffect = Content.Load<SoundEffect>("MySoundFile2");
soundEffectLoop = mySoundEffect.CreateInstance();
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
var touchState = TouchPanel.GetState();
bool touchDetected = touchState.Count > 0;
if ((!_screenPressed) && (touchDetected))
{
soundEffectLoop.Play();
}
else if ((_screenPressed)&&!(touchDetected))
{
soundEffectLoop.Stop();
}
_screenPressed = touchDetected;
base.Update(gameTime);
}
Next Section
In the next section, I will introduce XNA 3D rendering functionality.
CodeProject