mort8088 Just another WordPress site


MDX 9 – Tutorial 3

Originally Posted 2006

Background Music

Right so we've covered Sound-FX in the Last Tutorial is Time we're going to cover the area so often missed in Game tutorials, The Background music. This might not sound like a big deal but if you like me keep the background music on, it can add to the suspense of a situation like in DOOM3 or AvP, or it can make the adrenalin pump that little bit faster as you try to out run the cops like in the Need for Speed games. In short Music is every bit as important as the individual Sound-FX for setting your scene.

To start off open your project from the last tutorial and add another Class Library but this time call it "MusicFX". then add the same using lines that appear in fig 1.0.


fig 1.0

Straight off you should notice that we are using an extra library called "Microsoft.DirectX.AudioVideoPlayback" and it's this that makes it all work. Once again I've given DirectSound a short hand name of "Dsound". We're also using "System.IO" so that we can fetch the list of music files ourselves, I'll explain this more later when we need it.

Next we need our global data storage, so add the code from fig 2.0 to the class.


fig 2.0

The Audio object on line 12 is the one that makes all the noise. The songlist string array on line 14 is going to store the file & path for the songs we find. currentSong, totalSongs, & musicVolume are self-explanatory.


fig 3.0

The default constructor(fig 3.0) is empty because, well there really is nothing to do here. The initSongList on the other hand is where we get everything ready in fig 4.0.


fig 4.0

First up is the one and only use for "System.IO" this is just an option on how to get the songs in to the Class if you prefer the method we used in the Sound-FX tutorial then just change "String Dir" to "string[] Dir" and set songList directly that way you control the indexes for each song.

CurrentSong defaults to 0 as we're using a Zero based array. totalSongs is set to mark the top end of the songList array

Now, the Audio Object is set to the songObject variable using the first song in the array as the default song to play. if the songList array is NULL at this point things don't go well, that's not to say that if the songList is just empty things go wrong only if it's a NULL object.

Also we need to add an event handler to the songObject for when the current song is ending or we'll never know when the current song has ended so there will be a sudden lack of music, no crash, no fire and brimstone, just NO MUSIC!!!!!!

when the Audio object is first created it defaults to maximum volume quite why I subtract 600 I can't remember but we should lower the volume of the background music so we can hear the other sounds. we also need to track the current volume for when we change it so store it in musicVolume for later.


fig 5.0

Fig 5.0 was just added so I could get the name of the current song and it's place in the song list.


fig 6.0

Fig 6.0 and Fig 6.1 are overloads to do the same thing, Fig 6.0 simply checks that songList is not NULL, if everything is ok it opens the file that is in the currently selected index of songList. you still need to make a call to the PlaySong method that I'll go through in a second.


fig 6.1

Fig 6.1 is a little more complex you supply the fully qualified file name for the file and if its already in the array, its entry is used, if not the new file path is added at the end of the array and loaded anyway. Again you still need to call PlaySong method which leads me nicely into fig 7.0


fig 7.0

Now you may want me to go into more detail but I think you can see that by calling the PLAY method on the songObject it plays the song, we also reset the volume just for the hell of it.


fig 8.0

Are you seeing a pattern forming here by calling the songObject's STOP method the song magically stops playing.


fig 9.0

Well the nextSong & prevSong methods are a little more complex than the last two, but not by much.

Check the songList, stop the current song(fig 8.0) increment the song index and check it's not too big, open the next song(fig 6.0), play it(fig 7.0) done


fig 9.1

prevSong(fig 9.1) method is exactly the same just decrement the index and check it's not less than 0.


fig 10.0

PauseSong(fig 10.0) is as sickeningly straight forward as the Play and Stop methods.


fig 11.0

Remember that SongEnding event we set up way back in fig 4.0, well here it is in fig 11.0 in this class I went for the old play the next song in the list job, simple increment the index check it's not too big Open the new song play the new song, job done.


fig 12.0

With that all done I chucked in the dispose method just to be tidy.

Using this class is as straight forward as you'd think, create the instance of the class, init the song list, then play.

Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.