Выбрать главу

LoadSound

Once we have initialised the CSound object, we can use LoadSound to load a given file. The single parameter of LoadSound is the filename of the sound to load. This is not a path because all sounds will be loaded from the Sounds folder which is inside our project folder.

If the format of this CSound object is MP3, we first build up the full path of the file and convert it to a unicode string. Next, all we need to do is call the RenderFile method of the filter graph manager to construct a filter graph that will play the specified file.

If the format of this CSound object is WavMidi, we first convert the filename passed into a unicode string. Next we call LoadObjectFromFile which loads the file and returns a segment pointer. Lastly, we call the Download method of our segment which downloads the band data to the performance.

We are now ready to start playing sounds.

Play

Once a file has been loaded, we can play it. This single parameter of the Play method is optional (default is 0) and is the number of times you would like the sound to repeat. This parameter is only used if you are playing wav or midi files.

If the format of this CSound object is MP3, we first make sure that we are at the start of the stream by calling the put_CurrentPosition method of the media position object that we created earlier. Once this is done, we play the mp3 by calling the Run method of the media control object.

If the format of this CSound object is WavMidi, we first set the number of times that the sound should repeat. Then we play the loaded segment by calling the PlaySegmentEx method. If you would like the sound to repeat forever, pass in the constant DMUS_SEG_REPEAT_INFINITE.

Stop

This very simply stops the sound from playing. We use the Stop method of the media control object to stop the sound if it's an mp3 file. If it's a wav or midi file we simply call the StopEx method of the perfomance object passing in the segment to stop.

IsPlaying

Finally, the IsPlaying method will return true if the sound is playing and false if it is not. If the file is an mp3, we get the current position that playback has reached and the total duration of the track. If the current position is not at the end, then we must still be playing the sound. If the file is a wav or midi, we simply use the IsPlaying method of the performance object passing in the segment to check.

Cleaning up

In the CSound destructor we simply stop the sound from playing and release the objects. In CGame there is a new function called CleanUpDirectAudio which is shown below:

void CGame::CleanUpDirectAudio() {

 //Stop all sounds.

 m_pDirectAudioPerformance->Stop(NULL, NULL, 0, 0);

 //CleanUp

 m_pDirectAudioPerformance->CloseDown();

 SafeRelease(m_pDirectAudioLoader);

 SafeRelease(m_pDirectAudioPerformance);

 LogInfo("<li>CleanUpDirectAudio finished.");

}

Here we first stop all sounds from playing (just in case), then we close down the performance object before releasing it and the loader object. Also, in the CGame deconstructor we need to call CoUninitialize to close down the COM library (shown below).

CoUninitialize();

So now we can play music and sounds in our applications. When the application starts, the background mp3 file plays. Move the mouse and click on a number to hear a different "sound effect". The background mp3 track is just a short loop so that I could keep the download size low, you can replace this with a larger track if you wish.

Summary

In this tutorial we learnt how to play sounds and music using two new DirectX components, DirectX Audio and DirectShow. In the next tutorial we will use all of the skills we have seen so far to create our first game: 3D Pong. This game will be simple, but will have all of the key features that you would expect from any game.