Click here to Skip to main content
15,889,335 members
Articles / Mobile Apps / Windows Mobile

Introduction to Windows Phone Programming: Part 2

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
7 Sep 2010CPOL2 min read 17.3K   7  
Continuation of a blog post on Windows Phone 7 programming. This part covers playing sounds using XNA

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.

C#
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.

C#
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 exit
    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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering. I've largely developed solutions that are based on a mix of Microsoft technologies with open source technologies mixed in. I've got an interest in astronomy and you'll see that interest overflow into some of my code project articles from time to time.



Twitter:@j2inet

Instagram: j2inet


Comments and Discussions

 
-- There are no messages in this forum --