Archive for March 2011

Adding Audio to GSM + PSK

So a question came up in the forums about how to add a sound and some music to the menu in a combo of the GSM and PSK. I could have answered in the forums, but figured since I hadn’t made a post in a while I’d do it here.

Step 1 – get your sound files. I grabbed the NinjAcademy sample and pulled the Menu_Selection.wav and NinjAcademy_Music.wav files out of it.

Step 2 – add the sound files to your project. The easy way is to drag the files into the Content project in Visual Studio:


I changed the Asset Name property for the files to Menu and MenuMusic, just to make it easier:


I also changed the Content Processor property for NinjAcademy_Music.wav to Song, since we want the music to loop until the player either starts the game or exits. We’ll see why we had to do this in a second.

Step 3 – add code. Only two files have to change (at least in the way I did it) – MenuScreen.cs and MainMenuScreen.cs.

Open MenuScreen.cs and add this code somewhere:

protected virtual void SelectedMenuItemChanged()

This will allow us to play a sound when the user changes the selected menu item. There’s no code in it because we’re going to override it in the MainMenuScreen class, but since all the input handling occurs in this class we need to do this here.

In the HandleInput method call the new method in the if statements that check for the menu item changing:

// Move to the previous menu entry?
if (input.IsMenuUp(ControllingPlayer))

    if (selectedEntry < 0)
        selectedEntry = menuEntries.Count - 1;


// Move to the next menu entry?
if (input.IsMenuDown(ControllingPlayer))

    if (selectedEntry >= menuEntries.Count)
        selectedEntry = 0;



That’s all that’s needed in that class. Open the MainMenuScreen.cs file and add two new members to handle the sound and song at the very top of the class:

using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

namespace PlatformerGSM
    class MainMenuScreen : MenuScreen

        SoundEffect menuSelectionSound;
        Song menuSong;


Note the two using statements that have been added as well. If you don’t add them the code won’t compile as the SoundEffect class exists in the Audio namespace and the Song class exists in the Media namespace.

We need to load our files into these two members. This is done in the LoadContent method:


        public override void LoadContent()

            menuSelectionSound = ScreenManager.Game.Content.Load<SoundEffect>("Sounds/menu");
            menuSong = ScreenManager.Game.Content.Load<Song>("Sounds/menumusic");

            MediaPlayer.IsRepeating = true;



We start playing the menu music as soon as we load it, telling the MediaPlayer class that controls music to loop the song. The SoundEffect class doesn’t have this ability, which is why we had to use the Song class for the music. Note that we don’t have to create an instance of the MediaPlayer class. The class is declared as static.

The functionality to play the sound when the user changes the selected menu item is done by overriding the method we put in the MenuScreen class:

        protected override void SelectedMenuItemChanged()


One more task – we need to stop the music when the player starts playing or exits the game. A method already exists for the former, we just need to override it like we just did:

        protected override void OnSelectEntry(int entryIndex, PlayerIndex playerIndex)
            //only stop when the game starts
            if(entryIndex == 0)

            base.OnSelectEntry(entryIndex, playerIndex);


To stop the music when the player exits add the following line before the existing code in the ConfirmExitMessageBoxAccepted method in the MainMenuScreen class:



We don’t add the code in the event handler for the Exit menu item because the user may cancel exiting the game and we want the music to continue playing in that case.

That’s it. Run the game and make sure every thing works.

The source for this can be found here.

If you have any questions, the best place to ask is in the App Hub forums where I’m always at. I may not respond immediately to comments here, although I appreciate them. 🙂