This article describes how to add your own sound resources to the VC resource file so that your application can play the sound.
1. Use a text editor (such as NotePad) to open the resource file (. rc file)
Add your own sound Resources at the end, as shown in the following idw wave "c: // kav/sound // virus.wav"
Save the resource file.
2. Start another thread in the place where the sound is to be played in the application to play the sound:
| AfxBeginThread (AFX_THREADPROC) sound, NULL, THREAD_PRIORITY_NORMAL ); |
3. Add a thread callback function
This section describes how to load resources. First, use the function AfxGetInstanceHandle () to obtain the handle of the instance, and then use the function FindResource to find its own sound resource.
HRSRC FindResource ( HMODULE hModule, // module handle Lptstr lpName, // resource name LPCTSTR lpType // resource type ) |
After a sound resource is found, use LoadResource to add the resource.
HGLOBAL LoadResource ( HMODULE hModule, // module handle HRSRC hResInfo // resource handle ); |
Finally, lock the resource memory block and return the virtual memory address of the calibrated memory block. If the resource is successfully locked, the returned value points to the first byte at the beginning of the Resource:
| LPVOID pv = LockResource () |
Note: If a problem occurs in any of the above four steps, the system returns and releases the corresponding memory. The next step is to load data based on the file data type.
UINT CPlaySoundView: sound (LPVOID pParam) { HINSTANCE h = AfxGetInstanceHandle (); HRSRC hr = FindResource (h, "IDW", "WAVE "); HGLOBAL hg = LoadResource (h, hr ); LPSTR lp = (LPSTR) LockResource (hg ); SndPlaySound (lp, SND_MEMORY | SND_SYNC ); FreeResource (hg ); Return 0; } |