Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Introduction to Windows Phone Programming: Part 2

, 7 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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()
{
    // 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)

Share

About the Author

Joel Ivory Johnson
Software Developer Razorfish
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.
 
For the past few years I've been providing solutions to clients using Microsoft technologies for web and Windows applications.
 
While most of my CodeProject.com articles are centered around Windows Phone it is only one of the areas in which I work and one of my interests. I also have interest in mobile development on Android and iPhone. Professionally I work with several Microsoft technologies including SQL Server technologies, Silverlight/WPF, ASP.Net and others. My recreational development interest are centered around Artificial Inteligence especially in the area of machine vision.
 
Twitter:@J2iNet
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 7 Sep 2010
Article Copyright 2010 by Joel Ivory Johnson
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid