Time for action—reproducing music

Time for action—reproducing music


Great games have appealing background music. Now, we are going to search and add background music to our game:

Note

As with other digital content, sound and music have a copyright owner and a license. Hence, we must be very careful when downloading sound and music for our games. We must read licenses before deploying our games with these digital contents embedded.

  1. 1. One of the 3D digital artists found a very cool electro music sample for reproduction as background music. You have to pay to use it. However, you can download a free demo (Distorted velocity. 1) from http://www.musicmediatracks.com /music/Style/Electro/. Save the downloaded MP3 file (distorted_velocity._1.mp3) in the previously created media folder (C:\Silverlight3D\Invaders3D\Media).

    Note

    You can use any other MP3 sound for this exercise. The aforementioned MP3 demo is not included in the accompanying source code.

  2. 2. Stay in the 3DInvadersSilverlight project.

  3. 3. Right-click on the Media sub-folder in the 3DInvadersSilverlight.Web project and select Add | Existing item... from the context menu that appears.

  4. 4. Go to the folder in which you copied the downloaded MP3 file (C:\Silverlight3D\Invaders3D\Media). Select the MP3 file and click on Add. This way, the audio file will be part of the web project, in the Media folder, as shown in the following screenshot:

  5. 5. Now, add the following lines of code at the beginning of the btnStartGame button's Click event. This code will enable the new background music to start playing:

    // Background music
    MediaElement backgroundMusic = new MediaElement();
    LayoutRoot.Children.Add(backgroundMusic);
    backgroundMusic.Volume = 0.8;
    backgroundMusic.Source = 
    		new Uri("Media/distorted_velocity._1.mp3", UriKind.Relative);
    backgroundMusic.Play();
    
  6. 6. Build and run the solution. Click on the button and turn on your speakers. You will hear the background music while the transition effect starts.

What just happened?

You discovered that the speakers worked! Now, the game has attractive background music. Leave the speakers on, because your project manager wants more sound effects in the game.

We created a new MediaElement instance (backgroundMusic). However, this time, we used C# to create it, instead of working on XAML code. We had to add the new MediaElement to a parent container:

LayoutRoot.Children.Add(backgroundMusic);

Then, we defined the desired Volume level and the Source as a new relative Uri (Uniform Resource Identifier):

backgroundMusic.Volume = 0.8;
backgroundMusic.Source = new Uri("Media/distorted_velocity._1.mp3",
UriKind.Relative);

The Volume ranges from 0 to 1. It uses a linear scale. We used 0.8 because we want the future sound effects to be louder than the background music.

The first parameter for the new Uri is the relative path (ClientBin is our base path in the web project). The second one is the UriKind. In this case, we are working with a Relative Uri.

Once we set up all the necessary parameters, we called the Play method and Silverlight started playing the MP3 file:

backgroundMusic.Play();

The code goes on running while the music file is being played. Hence, the game starts and we can still hear the music.