IntroductionI'm writing a rather lengthy piece on Windows Phone programming and rather than wait for the entire piece to be completed I've decided to start making the sections available in draft form. My intent is to cover XNA (2D and 3D) and Silverlight concepts.This is part two. you can read part one here. ImplementationAdding SoundThe 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 affects from some event happening). Let's start of 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 suggesd 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. Code 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 begining 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. Code protected override void LoadContent()
{// Create a new SpriteBatch, which can be used to draw textures.spriteBatch = new SpriteBatch(GraphicsDevice);
mySoundEffect = Content.Load<SoundEffect>("MySoundFile2");
soundEffectLoop = mySoundEffect.CreateInstance();
}
protected override void Update(GameTime gameTime)
{// Allows the game to exitif (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);
}That's all in this section, in the next part "I will introduce XNA 3D rendering functionality". |