Add a sound resource to a resource file

Source: Internet
Author: User
Tags thread

This article describes how to add your own sound resources to the VC resource file so that your application can play the sound.

1, first open the resource file (. rc file) with a text editor (such as Notepad)

In the end add your own sound resources, as follows

IDW WAVE "c:\\kav\\sound\\virus.wav"

Save the resource file.

2, to start another thread in the application where you want to play the sound, to play the sound

AfxBeginThread((AFX_THREADPROC)sound,NULL,THREAD_PRIORITY_NORMAL);

3, add the thread callback function

Here's how to load a resource. First, the handle of the instance is obtained by the function AfxGetInstanceHandle (), and then the sound resource is found by using the function FindResource.

HRSRC FindResource(
 HMODULE hModule, // module handle
 LPCTSTR lpName, // resource name
 LPCTSTR lpType  // resource type
)

After you find the sound resource, use LoadResource to join the resource

HGLOBAL LoadResource(
 HMODULE hModule, // module handle
 HRSRC hResInfo  // resource handle
);

Finally, the resource memory block is locked and the virtual memory address of the memory block that is calibrated is returned. If the resource is successfully locked, the return value points to the first byte at the beginning of the resource:

LPVOID pv=LockResource()

Note: If the problem occurs in any of the four steps above, return and release the corresponding memory. The next thing to do 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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.