How to play wav files in VC

Source: Internet
Author: User

The playsound function is used to play wav files in VC.
1. Function details:
The playsound function is declared as follows:
Bool playsound (lpcstr pszsound, hmodule hmod, DWORD fdwsound );
The parameter pszsound is a string that specifies the sound to be played. This parameter can be the name of the wave file, the name of the wav resource, the pointer to the sound data in the memory, or win in the system registry. system Event sound defined in ini. If this parameter is null, the playing sound is stopped. The hmod parameter is the application instance handle. this parameter is used when playing wav resources; otherwise, it must be null. The fdwsound parameter is a combination of flags, as shown in the following table. If the call succeeds, the function returns true; otherwise, false.

Playing marks and meanings:
Snd_application
Play 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 a predefined sound identifier.
Snd_async
Play the sound asynchronously. The playsound function returns the result immediately after the playing starts.
Snd_filename
The pszsound parameter specifies the wave file name.
Snd_loop
The replay sound must be used with the snd_async flag.
Snd_memory
Play the sound loaded into the memory. In this case, pszsound is a pointer to the sound data.
Snd_nodefault
Do not play the default sound. If this flag is not displayed, 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
Stop all sounds related to the call task. If the parameter pszsound is null, all sound is stopped. Otherwise, the sound specified by pszsound is stopped.
Snd_resource
The pszsound parameter is the identifier of the wave resource, and the hmod parameter is used.
Snd_sync
The playsound function returns the synchronous playback sound after playback.

2. Usage:
There is a sound file named the Microsoft sound.wav in the C:/Windows/Media Directory, which is played when Windows 95 is started. Below we use three methods to call the playsound function to broadcast the Startup Sound of Windows 95.

The first method is to directly broadcast the sound file, and the corresponding code is:
Playsound ("C:/Win95/Media/the Microsoft sound.wav", null, snd_filename | snd_async );

Note that the path in the parameter uses two consecutive backslash escape 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. You can right-click the resource view and select the import command. Then, in the file selection dialog box, select the Microsoft sound.wav file, the file will be added to the wave resource. Assuming that the sound resource ID is idr_startwin, the following call also outputs the Startup sound:

Playsound (lpctstr) idr_startwin, AfxGetInstanceHandle (), snd_resource | snd_async );

The third method is to use playsound to play the system sound. The Windows Startup Sound is defined by systemstart. Therefore, you can use the following method to play the Startup sound:
Playsound ("systemstart", null, snd_alias | snd_async );
The function sndplaysound is similar to playsound, but one parameter is missing. Function declaration:
Bool sndplaysound (lpcstr lpszsound, uint fusound );
Except that the resource name cannot be specified, the parameter lpszsound and playsound are the same. The fusound parameter indicates how to play a sound. It can be a combination of snd_async, snd_loop, snd_memory, snd_nodefault, snd_nostop, and snd_sync. These marks have the same meanings as playsound.

We can see that sndplaysound cannot directly play audio resources. To use this function to play the wave file, you can call the following method:
Sndplaysound ("mysound. wav", snd_async );

Note: when using the playsound function, you need to add winmm. lib and the header file mmsystem. h.
Setting method:
In the VC compiler, choose Project> setting> link> Object/library modules and add winmm. lib to the edit box.
Add the statement # include <mmsystem. h> to stdafx. h.

The playsound function is used to play wav files in VC.
1. Function details:
The playsound function is declared as follows:
Bool playsound (lpcstr pszsound, hmodule hmod, DWORD fdwsound );
The parameter pszsound is a string that specifies the sound to be played. This parameter can be the name of the wave file, the name of the wav resource, the pointer to the sound data in the memory, or win in the system registry. system Event sound defined in ini. If this parameter is null, the playing sound is stopped. The hmod parameter is the application instance handle. this parameter is used when playing wav resources; otherwise, it must be null. The fdwsound parameter is a combination of flags, as shown in the following table. If the call succeeds, the function returns true; otherwise, false.

Playing marks and meanings:
Snd_application
Play 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 a predefined sound identifier.
Snd_async
Play the sound asynchronously. The playsound function returns the result immediately after the playing starts.
Snd_filename
The pszsound parameter specifies the wave file name.
Snd_loop
The replay sound must be used with the snd_async flag.
Snd_memory
Play the sound loaded into the memory. In this case, pszsound is a pointer to the sound data.
Snd_nodefault
Do not play the default sound. If this flag is not displayed, 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
Stop all sounds related to the call task. If the parameter pszsound is null, all sound is stopped. Otherwise, the sound specified by pszsound is stopped.
Snd_resource
The pszsound parameter is the identifier of the wave resource, and the hmod parameter is used.
Snd_sync
The playsound function returns the synchronous playback sound after playback.

2. Usage:
There is a sound file named the Microsoft sound.wav in the C:/Windows/Media Directory, which is played when Windows 95 is started. Below we use three methods to call the playsound function to broadcast the Startup Sound of Windows 95.

The first method is to directly broadcast the sound file, and the corresponding code is:
Playsound ("C:/Win95/Media/the Microsoft sound.wav", null, snd_filename | snd_async );

Note that the path in the parameter uses two consecutive backslash escape 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. You can right-click the resource view and select the import command. Then, in the file selection dialog box, select the Microsoft sound.wav file, the file will be added to the wave resource. Assuming that the sound resource ID is idr_startwin, the following call also outputs the Startup sound:

Playsound (lpctstr) idr_startwin, AfxGetInstanceHandle (), snd_resource | snd_async );

The third method is to use playsound to play the system sound. The Windows Startup Sound is defined by systemstart. Therefore, you can use the following method to play the Startup sound:
Playsound ("systemstart", null, snd_alias | snd_async );
The function sndplaysound is similar to playsound, but one parameter is missing. Function declaration:
Bool sndplaysound (lpcstr lpszsound, uint fusound );
Except that the resource name cannot be specified, the parameter lpszsound and playsound are the same. The fusound parameter indicates how to play a sound. It can be a combination of snd_async, snd_loop, snd_memory, snd_nodefault, snd_nostop, and snd_sync. These marks have the same meanings as playsound.

We can see that sndplaysound cannot directly play audio resources. To use this function to play the wave file, you can call the following method:
Sndplaysound ("mysound. wav", snd_async );

Note: when using the playsound function, you need to add winmm. lib and the header file mmsystem. h.
Setting method:
In the VC compiler, choose Project> setting> link> Object/library modules and add winmm. lib to the edit box.
Add the statement # include <mmsystem. h> to stdafx. h.

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.