Current location - Music Encyclopedia - QQ Music - How to insert music into C language programming?
How to insert music into C language programming?

1. Play sound files directly in the program \x0d\ The multimedia dynamic link library in VC++ provides a set of functions related to audio devices. Use these functions to play sounds conveniently. The simplest way to play sound is to directly call the sound playback function BOOL sndPlaySound (LPCSTR lpszSound, UINT fuSound) provided in VC++; or BOOL PlaySound (LPCSTR lpszSound, HMODULE hmod, DWORD fuSound); where the parameter lpszSound is required to play the sound.W ***The path and file name of the file, hmod is NULL here, fuSound is the flag for playing sound, please refer to the help in VC++ for detailed instructions. For example, to play C:soundmusic.wav, you can use sndPlaySound ("c:\sound\music.wav",SND_ASYNC); or PlaySound("c:\sound\music.wav",NULL, SND_ASYNC|SND_NODEFAULT); if music is not found .wav file, the first format will play the system default sound, and the second format will not play the system default sound. \x0d\\x0d\ 2. Add sound files to the program \x0d\ In VC++ programming, you can use various standard resources, such as bitmaps, menus, dialog boxes, etc. At the same time, VC++ also allows users to customize resources, so we can add sound files as user-defined resources to program resource files, and generate EXE files through compilation and connection to achieve sound playback without .W*** files. To play sound files as resources, you must first add the sound files to be played in the resource manager.

\x0d\The specific steps are as follows:\x0d\ 1. Get the module handle containing the resource:\x0d\ HMODULE hmod=AfxGetResourceHandle();\x0d\ 2. Retrieve resource block information: \x0d\ HRSRC hSndResource=FindResource(hmod,MAKEINTRESOURCE(IDR_W***E1),_T("W***E"));\x0d\ 3. Load resource data and lock:\x0d \ HGLOBAL hGlobalMem=LoadResource(hmod,hSndResource);\x0d\LPCTSTR lpMemSound=(LPCSTR)LockResource(hGlobalMem);\x0d\ 4. Play sound files: \x0d\ sndPlaySound(lpMemSound,SND_MEMORY)); \x0d\ 5. Release resource handle:\x0d\ FreeResource(hGlobalMem);