{steinsoft.net}
 
 
 
 
 
Home/News
Code Snippets
Board
Projects
What's That?
I am using it actively
I planning to use/learn it
I don't plan to lean/use it
5th option
Leipzig
Home :: Programming :: Code Snippets :: MfcWin :: Playing Soundfiles

[ Playing Soundfiles ]

If you want to play little sound files and you don't need maximum power than this snippet is right for you. I will show you a standard Win32 function that plays Wave audio files no matter whether the file is a a normal file or a resource file. I will also show you how to preload the file and play it afterwards. This function is not as powerful as DirectSound however it is easier to use and it is sometimes good enough.

The funtion we will be using is as follow:

BOOL PlaySound( LPCSTR pszSound, HMODULE hmod, DWORD fdwSound );

PlaySound returns true when the file has been played successfully and false if there was an error. Here are three little examples how to use it:

//*******************
// Example Nr.1
//*******************
//sound.wav will beplayed ansynchronousely
//As it is a file on the HD, SND_FILENAME must be set
PlaySound("sound.wav",NULL,SND_FILENAME | SND_ASYNC);
 
//*******************
// Example Nr.2
//*******************
//Play the file from memory
//Load soundfile
ifstream file(filenameOfWave);
file.seekg(0,ios::end);
int imageLength = file.tellg();
 
//Holds the image of the soundfile
char* imageData = new char[imageLength];
 
file.seekg(0,ios::beg);
file.read(imageData,imageLength);
 
//Finally play the file
PlaySound((LPCSTR)imageData, NULL, SND_MEMORY | SND_ASYNC);
//When you don't need the audio file anymore,
// => delete[] imageData; !! We don't want memory leaks;)
 
//*******************
// Example Nr.3
//*******************
//Add a sound-resource IDS_SOUND to your project and
//try this out
//Will be played looped
PlaySound(MAKEINTRESOURCE(IDS_SOUND),AfxGetInstanceHandle(),
          SND_RESOURCE | SND_ASYNC | SND_LOOP);

The first example simply plays a file from the HD. Nothing special.

In the second example we first load the complete file to memory and then we call PlaySound(..). The first parameter points here to the image of the sound file. Notice that this method is much faster than loading the file every time from disk. Load the file into memory when your app starts and destroy the memory block when the app quits. But don't load the file into memory every time you play the sound, that would be non-sense..

In the last example we play a soundfile from the app's resources. Remember that you have to set the second parameter - the application's handle - before playing resource files. I use the Mfc function AfxGetInstanceHandle() here. If you use the standard Win32 API, use GetModuleHandle(NULL).

That's all! If you don't want to use DirectSound, this method works very well! Questions? Comments? M@il Me. Happy Coding!

 Last edited 2004-06-30 20:26:56 by André Stein - printable version
» copyright by andré stein
» using stCM v1.0
» steinsoft.net revision 5.0