Index   Commented   Search   About

Playing WAVE Resources

You can use the PlaySound function to play a sound that is stored as a resource. Although this is also possible using the sndPlaySound function, sndPlaySound requires that you find, load, lock, unlock, and free the resource; PlaySound achieves all of this with a single line of code.

PlaySound Example

PlaySound("SoundName", hInst, SND_RESOURCE | SND_ASYNC);

sndPlaySound Example

The SND_MEMORY flag indicates that the lpszSoundName parameter is a pointer to an in-memory image of the WAVE file. To include a WAVE file as a resource in an application, add the following entry to the application's resource script (.RC) file.

soundName WAVE c:\sounds\bells.wav

The name soundName is a placeholder for a name that you supply to refer to this WAVE resource. WAVE resources are loaded and accessed just like other application-defined Windows resources. The PlayResource function in the following example plays a specified WAVE resource.

BOOL PlayResource(LPSTR lpName)

{

BOOL bRtn;

LPSTR 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 by using this function, pass to the function a pointer to a string containing the name of the resource, as shown in the following example.

PlayResource("soundName");

Related Links

Software for Delphi and C++ Builder developers
Software for Visual Studio .NET developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET
TMS Scripter Studio Pro components for Delphi/C++Builder

More Online Helps

Win32 Programmer's Reference (win32.hlp)
OLE Programmer's Reference (ole.hlp)
Microsoft Windows Pen API Programmer's Reference (penapi.hlp)
Microsoft Windows Sockets 2 Reference (sock2.hlp)
Microsoft Windows Telephony API (TAPI) Programmer's Reference (tapi.hlp)
Unix Manual Pages

Free Tech Secrets ;) Copyright © 2008 Free Tect Secrets ;) greatis just4fun network just4fun