MFC playback WAV audio files _mfc

Source: Internet
Author: User


First, your resources should be in WAV format and include header files and required library files as follows
#include <mmsystem.h>
#pragma comment (lib, "Winmm.lib")


The declaration of the PlaySound function is:

BOOL PlaySound (lpcstr pszsound, hmodule hmod,dword fdwsound);

PlaySound (null,null,null); stop playing.
 

The parameter pszsound is a string that specifies the sound to play (generally the absolute path, if the sound file is copied to the executable file path can be directly using the sound file name), this parameter can be the name of the wave file, or the name of the WAV resources, or in-memory voice data pointers, or a system event sound defined in the system registry win.ini. If this argument is null, the sound that is playing is stopped. A parameter hmod is an instance handle of an application that must be used when playing a WAV resource, otherwise it has to be null. Parameter fdwsound is a combination of flags, as shown in the following table. If successful, the function returns True, otherwise it returns false.

Playback flags and meanings:

Snd_application
Plays a sound with an application-specified association.

Snd_alias
The Pszsound parameter specifies the alias of a system event in the registry or Win.ini.

snd_alias_id
The Pszsound parameter specifies a 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 filename.

Snd_loop
Play the sound repeatedly, it must be used with the SND_ASYNC flag.

Snd_memory
Play a sound that is loaded into memory, at which point Pszsound is a pointer to the sound data.

Snd_nodefault
Do not play the default sound, without this flag, PlaySound will play the default sound when no sound is found.

Snd_nostop
PlaySound does not interrupt the original sound broadcast and returns false immediately.

Snd_nowait
If the driver is busy, the function does not play the sound and returns immediately.

Snd_purge
Stops all sounds associated with 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 for the wave resource, and the Hmod parameter is used.

Snd_sync
Play the sound synchronously, and the PlaySound function returns after the play is finished.

 

 

There is a sound file named the Microsoft sound.wav in the C:\WINDOWS\MEDIA directory that will play this sound when WINDOWS 95 starts. Here are three ways to invoke the PlaySound function to broadcast the startup sound of Windows 95.

The first method is to broadcast the sound file directly, the corresponding code is:

PlaySound ("C:\\win95\\media\\the Microsoft sound.wav", NULL, Snd_filename | Snd_async);

Note the path in the argument uses two consecutive backslash escapes to represent a backslash.

The second method is to add the sound file to the resource and then play the sound from the resource. Visual C + + supports wave resources, the user clicks the right mouse button in the resource view and selects the Import command, and then selects the Microsoft sound.wav file in the File Selection dialog box, the file is added to the wave resource. Assuming the ID of the sound resource is Idr_startwin, the following call will also output the startup sound:

PlaySound ((LPCTSTR) Idr_startwin, AfxGetInstanceHandle (), Snd_resource | Snd_async);

The third approach is to play the system sound with PlaySound, which 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 the PlaySound, except that the resource name cannot be specified. Parameter fusound is a sign of how 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 the sound resource directly. To play a wave file with this function, you can invoke it in the following way:

sndPlaySound ("Mysound.") WAV ", Snd_async);

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.