A A simple way to play a sound file
A set of functions related to audio devices is provided in the multimedia Dynamic Connection library in VC + +. These functions make it easy to play sounds. The simplest way to play sounds is to directly call VC + + provided in the sound Play function bool sndPlaySound (LPCSTR lpszsound,uint fusound); or bool PlaySound (LPCSTR lpszsound, hmodule hmod, DWORD fusound), where the parameter lpszsound is the path and file name of the. wav file that needs to play the sound, Hmod here is null, Fusound is a sign of playing sound, please refer to VC + + Help for details. For example, play C:\sound\music.wav can be used with sndPlaySound ("C:\\sound\\music.wav", Snd_async), or PlaySound ("C:\\sound\\music.wav", NULL , snd_async| Snd_nodefault); If the Music.wav file is not found, the first format will play the system default sound, and the second format will not play the system default sound.
two. Add a sound file to your program
in VC + + program design, you can use a variety of standard resources, such as bitmaps, menus, dialog boxes. At the same time, VC + + also agreed to user-defined resources, so we can use the sound file as a user-defined resource to join the program resource file, compiled connection generated EXE file, to achieve none. WAV file for sound playback.
to implement the playback of a sound file as a resource, first add the sound file to be played in the explorer (the implementation process is not complicated and is not described here). Assume that the generated sound file resource identifier is idr_wave1. You only need to call the following statement when playing:
PlaySound (Makeintresource (idr_wave1), AfxGetResourceHandle (), snd_async| snd_resource| Snd_nodefault| Snd_loop);
where the Makeintresource () macro turns the integer resource identifier into a string, and the AfxGetResourceHandle () function returns the module handle containing the resource,
Snd_resource is a required flag. The second method of playing the
sound file as a resource is to read the resource into memory and play it as memory data. Concrete steps into the following:
1. Get the module handle containing the resource:
hmodule hmod=afxgetresourcehandle ();
2. Retrieving resource block information:
Hrsrc hsndresource=findresource (Hmod,makeintresource (idr_wave1), _t ("WAVE"));
3. Load the resource data and lock it:
Hglobal hglobalmem=loadresource (hmod,hsndresource);
LPCTSTR lpmemsound= (LPCSTR) Lockresource (HGLOBALMEM);
4. Play sound file:
sndPlaySound (lpmemsound,snd_memory));
5. Release resource handle:
FreeResource (HGLOBALMEM);
The third method is to play the system sound with PlaySound, and the Windows-initiated sound is a system sound defined by Systemstart, so you can play the startup sound in the following ways:
PlaySound ("Systemstart", null,snd_alias| Snd_async);
function sndPlaySound function is similar to PlaySound, but one parameter is missing. The declaration of the function is:
BOOL sndPlaySound (LPCSTR lpszsound, UINT fusound);
The parameter lpszsound is the same as PlaySound except that the resource name cannot be specified. The parameter fusound is a sign of how the sound is played, and can be a combination of Snd_async, Snd_loop, Snd_memory, Snd_nodefault, Snd_nostop, and Snd_sync, which have the same meaning as PlaySound.
As you can see, sndplaysound cannot play sound resources directly. To play a wave file with this function, you can call it as follows
=================================================================================
The declaration of the PlaySound function is:
BOOL PlaySound (LPCSTR pszsound,hmodule hmod,dword fdwsound);
Parameter description:
Pszsound: is a string that specifies the sound to play, which can be the name of a wave file, the name of a wave resource, a pointer to a sound data in memory, or a system event sound defined in the system registry Win.ini. If the parameter is null, stops the sound that is playing .
Hmod: is an instance handle to the application that is used when playing a WAV resource, otherwise it must be null.
Fdwsound: is a combination of flags, as shown in the following table. The function returns true if successful, otherwise false is returned.
Second, play logo and meaning:
Snd_application
Plays the sound using the association specified by the application.
Snd_alias
The Pszsound parameter specifies the alias of the system event in the registry or Win.ini.
snd_alias_id
The Pszsound parameter specifies the predefined sound identifier.
Snd_async
Plays the sound asynchronously, and the PlaySound function returns immediately after it starts playing.
Snd_filename
The Pszsound parameter specifies the wave file name.
Snd_loop
Play the sound repeatedly and must be used with the SND_ASYNC flag.
Snd_memory
Plays a sound loaded into memory, at which point the pszsound is a pointer to the sound data.
Snd_nodefault
The default sound is not played, and if this flag is not available, PlaySound plays the default sound when no sound is found.
Snd_nostop
PlaySound does not interrupt the original sound to broadcast and immediately returns false.
Snd_nowait
If the driver is busy, the function does not play the sound and returns immediately.
Snd_purge
Stops all sounds related to the calling task. If the parameter pszsound is null, stop all sounds, otherwise stop pszsound the specified sound.
Snd_resource
The Pszsound parameter is the identifier of the wave resource, and the Hmod parameter is used.
Snd_sync
Plays the sound synchronously, and the PlaySound function returns after the play is finished.
Three, the function uses the method and the code:
Attention: To join before using the function:
#include "mmsystem.h"//import Sound header file
#pragma comment (lib, "Winmm.lib")//import sound header file Library
1. Direct broadcasting of sound files:
PlaySound ("C:\\win95\\media\\the Microsoft sound.wav", NULL, Snd_filename | Snd_async);
Note: Paths in parameters use two successive backslash escapes to represent a backslash.
2. Add the sound file to the resource, and then play the sound from the resource:
Visual C + + supports wave resources, where users right-click in the Resource View and select the Import command, and then select the Microsoft sound.wav file in the File Selection dialog box, the file is added to the wave resource. Assuming that the sound resource's ID is Idr_startwin, the following call will also output the start sound:
PlaySound (LPCTSTR) Idr_startwin, AfxGetInstanceHandle (), Snd_resource | Snd_async);
Or:
PlaySound (Makeintresource (idr_wave2), AfxGetResourceHandle (), snd_async| snd_resource| Snd_nodefault| Snd_loop)//write a sound file into the program
3. Use PlaySound to play the system sound:
Windows-initiated sounds are system sounds defined by Systemstart, so you can play the startup sound in the following ways:
PlaySound ("Systemstart", null,snd_alias| Snd_async);
The declaration of the sndPlaySound function is:
BOOL sndPlaySound (LPCSTR lpszsound, UINT fusound);
Usage:
The parameter lpszsound is the same as PlaySound except that the resource name cannot be specified. The parameter fusound is a sign of how the sound is played, and can be a combination of Snd_async, Snd_loop, Snd_memory, Snd_nodefault, Snd_nostop, and Snd_sync, which have the same meaning as PlaySound.
As you can see, sndplaysound cannot play sound resources directly. To play a wave file with this function, you can call it as follows:
sndPlaySound ("Mysound. WAV ", Snd_async);
Several methods of playing sound in MFC