To play a sound stored as a resource, use the PlaySound function. Although you can use sndPlaySound to play a resource sound, you must find, load, lock, unlock, and free the resource; in contrast, PlaySound completes these tasks in a single line of code.
soundName WAVE \sounds\bells.wav
The name soundName is a placeholder for a name you supply to refer to the wave resource sound. Wave resources are loaded and accessed like other application-defined Windows resources.
The following code example shows how to use the PlaySound function to play the wave resource sound.
PlaySound (TEXT("soundName"), hInst, SND_RESOURCE | SND_ASYNC);
In contrast, the following code example shows how to use the sndPlaySound function to play a wave resource sound.
BOOL PlayResource (LPTSTR lpName)
{
BOOL bRtn;
LPTSTR lpRes;
HANDLE hResInfo, hRes;
// Find the wave resource.
hResInfo = FindResource (hInst, lpName, "WAVE");
if (hResInfo == NULL)
return FALSE;
// Load the wave resource.
hRes = LoadResource (hInst, hResInfo);
if (hRes == NULL)
return FALSE;
// Lock the wave resource and play it.
lpRes = LockResource (hRes);
if (lpRes != NULL)
{
bRtn = sndPlaySound (lpRes, SND_MEMORY | SND_SYNC | SND_NODEFAULT);
UnlockResource (hRes);
}
else
bRtn = 0;
// Free the wave resource and return success or failure.
FreeResource (hRes);
return bRtn;
}
To play a wave resource sound by using sndPlaySound, pass the function a pointer to a string containing the resource name, as shown in the following code example.
PlayResource (TEXT("soundName"));