Blake O'Hare .com

Gamelight Tutorial: Part 4 - Sound and Music

The next major component of games after interactivity and graphics is sound. Luckily, sounds and music file resources work pretty much the same way as images. They must be added to your project as an embedded resource. Once you do that, then you instantiate music as a Gamelight.Sounds.Music instance and sound effects as a Gamelight.Sounds.Sound instance.

Difference between Music and Sound objects:

Although the underlying implementation is mostly the same, the music instance has support for looping and transitions. Only one music file can be playing at a time. Although there is an exception if one song is fading out and you want the next song to fade in simultaneously.

Multiple sounds can play at the same time. There is, however, a limit. If you try to play too many sounds at once, it'll cut off the earlier sounds as they are still playing. But this is only likely to happen if you try to play a few dozen very long sounds at the same time.

Playing Sound Effects

To instantiate a sound and play it, simply do the following:
Sound sound = new Sound("sfx/zoom.wma");

Sound and SoundManager classes (along with the Music class) are located in the Gamelight.Sound namespace.

Background Music

Music, is quite similar. However, there are a few more customizeable options:
Music bgmusic = new Music("music/polka.mp3");
bgmusic.Loop = MusicLoopStyle.LoopForever;
bgmusic.Transition = MusicTransitionStyle.FadeOutCurrentFadeInThisSimultaneously;
bgmusic.TransitionSeconds = 3.0;

The Loop property can have two different values, LoopForever and PlayOnce. If LoopForever is set, the song will start playing again once it reaches the end.
The value of the Transition property will determine how this song will interrupt the currently playing song if there is one. The TransitionsSeconds property will specify how long the Transition will take.
The values for MusicTransitionStyle are as follows:
  • FadeOutCurrentAndPlay - fades out the currently playing song and then will start this song.
  • FadeOutCurrentAndStop - will fade out the currently playing song and will NOT play this one. This usually isn't very useful.
  • Instant - will interrupt the currently playing song and play this one immediately.
  • FadeOutCurrentFadeInThisSimultaneously - will fade out the currently playing song for Transition seconds while fading in this song at the same time.

Shut up

Additionally, you can stop the current music with SoundManager.StopMusic(). StopMusic() optionally takes in a double for the number of seconds to fade the music out.

Multiple Menus/Scenes with the same desired music

In many game architectures, there will be multiple scenes/menus that use the same music, but there will be many entry points into that set of scenes/menus. If you place a call to PlayMusic at the beginning of each scene/menu, then it will restart the music each time you transition from menu to menu, etc. which is bad. In cases like this, where you don't want to restart the song if it's already playing, use the SoundManager.EnsureMusicPlaying function which works just like PlayMusic with the exception that it will do nothing if the music currently playing is the same as the music you pass in.

Supported formats are everything the MediaElement supports, which are MP3's and WMA's.

Now go make some noise.