Overview of the Windows Phone 7 Programming: Tutorial -Part 2

No.of Views1758
Bookmarked0 times
Downloads 
Votes0
By  j2inet   On  20 Nov 2010 04:11:08
Tag : Windows Phone , General
I'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.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

I'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.

Implementation

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 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".

 

 
Sign Up to vote for this article
 
About Author
 
j2inet
Occupation-Not Provided
Company-Not Provided
Member Type-Senior
Location-Not Provided
Joined date-12 Jun 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
    You've got a class that contains data that you want to be able to save and load in IsolatedStorage on Windows Phone 7. How would you go about doing that? There is more than one way, but I wanted to share a generic solution for didactic purposes; I'm finding that many beginners like to start off with a single satisfying solution and brance off into specialized solutions later. Let's say that you are starting off with a stereotypical Employee class.
    Published Date : 05/Mar/2011
    In this article, i'm going to show to you,How to get System Information on Windows Phone 7
    Published Date : 06/Nov/2010
    In this article, i'm going to show to you, How to use DynameicSoundEffectInstance in Windows Phone 7
    Published Date : 06/Nov/2010
    In this snippet, i will show how to check Media element is locked or not windows phone using C#.
    Published Date : 10/Dec/2010
    This article will get a .Net developer started with Windows Phone 7 development and introduce the basic application models and services available in Windows Phone 7 and more.
    Published Date : 10/Nov/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top