다음을 통해 공유


Using PlaySound with a Resource Identifier (Windows Embedded CE 6.0)

1/6/2010

To play a sound that is stored as a resource, use the PlaySound function. Although you can use the sndPlaySound function 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.

To include a .wav file as a resource in an application

  • Add the following entry to the resource script (.rc) file of the application.

    soundName WAVE \sounds\bells.wav
    

    The soundName parameter is a placeholder for a name that 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 a 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); 
    } 
  else 
    bRtn = 0; 

  return bRtn; 
}

To play a wave resource sound by using sndPlaySound, pass into the function a pointer to a string that contains the resource name, as shown in the following code example.

PlayResource (TEXT("soundName"));

See Also

Concepts

Using the PlaySound Function