Windows API函数之PlaySound函数以及sndPlaySound函数用法总结

来源:互联网 发布:建筑平面图纸怎么算法 编辑:程序博客网 时间:2024/05/17 09:25

     本人在wince应用程序编程中遇到了播放关机音乐的问题,还有同步播放和异步播放的问题,这些问题主要是PlaySound函数的问题。现将相关内容整理如下。

 

一、PlaySound函数用法

PlaySound函数的声明为:
BOOL PlaySound(LPCSTR pszSound, HMODULE hmod,DWORD fdwSound);


1、参数pszSound是指定了要播放声音的字符串,该参数可以是WAVE文件的名字,或是WAV资源的名字,或是内存中声音数据的指针,或是在系统注册表WIN.INI中定义的系统事件声音。如果该参数为NULL则停止正在播放的声音。

 

2、参数hmod是应用程序的实例句柄,当播放WAV资源时要用到该参数,否则它必须为NULL。参数fdwSound是标志的组合,如下表所示。若成功则函数返回TRUE,否则返回FALSE。

 

3、播放标志以及含义:

SND_APPLICATION
用应用程序指定的关联来播放声音。

SND_ALIAS
pszSound参数指定了注册表或WIN.INI中的系统事件的别名。

SND_ALIAS_ID
pszSound参数指定了预定义的声音标识符。

SND_ASYNC
用异步方式播放声音,PlaySound函数在开始播放后立即返回。

SND_FILENAME
pszSound参数指定了WAVE文件名。

SND_LOOP
重复播放声音,必须与SND_ASYNC标志一块使用。

SND_MEMORY
播放载入到内存中的声音,此时pszSound是指向声音数据的指针。

SND_NODEFAULT
不播放缺省声音,若无此标志,则PlaySound在没找到声音时会播放缺省声音。

SND_NOSTOP
PlaySound不打断原来的声音播出并立即返回FALSE。

SND_NOWAIT
如果驱动程序正忙则函数就不播放声音并立即返回。

SND_PURGE
停止所有与调用任务有关的声音。若参数pszSound为NULL,就停止所有的声音,否则,停止pszSound指定的声音。

SND_RESOURCE
pszSound参数是WAVE资源的标识符,这时要用到hmod参数。

SND_SYNC
同步播放声音,在播放完后PlaySound函数才返回。


4、举例:

    在C:/WINDOWS/MEDIA目录下有一个名为The Microsoft Sound.wav的声音文件,在Windows 95启动时会播放这个声音。下面我们用三种方法来调用PlaySound函数播出Windows 95的启动声音。

第一种方法是直接播出声音文件,相应的代码为:

PlaySound("c://win95//media//The Microsoft Sound.wav", NULL, SND_FILENAME | SND_ASYNC);

注意参数中的路径使用两个连续的反斜杠转义代表一个反斜杠。

第二种方法是把声音文件加入到资源中,然后从资源中播放声音。Visual C++支持WAVE型资源,用户在资源视图中单击鼠标右键并选择Import命令,然后在文件选择对话框中选择The Microsoft Sound.wav文件,则该文件就会被加入到WAVE资源中。假定声音资源的ID为IDR_STARTWIN,则下面的调用同样会输出启动声音:

PlaySound((LPCTSTR)IDR_STARTWIN, AfxGetInstanceHandle(), SND_RESOURCE | SND_ASYNC);

第三种方法是用PlaySound播放系统声音,Windows启动的声音是由SystemStart定义的系统声音,因此可以用下面的方法播放启动声音:

PlaySound("SystemStart",NULL,SND_ALIAS|SND_ASYNC);

 

 

5、MSDN上关于PlaySound这个函数的说明如下:


This function plays a sound specified by a file name, resource, or system event.

 

BOOL WINAPI PlaySound(
  LPCSTR pszSound,
  HMODULE hmod,
  DWORD fdwSound
);

 

Parameters


pszSound
Pointer to a null-terminated string that specifies the sound to play. If this parameter is NULL, any currently playing waveform sound is stopped. To stop a non-waveform sound, specify SND_PURGE in the fdwSound parameter.
Three flags in fdwSound (SND_ALIAS, SND_FILENAME, and SND_RESOURCE) determine whether the name is interpreted as an alias for a system event, a file name, or a resource identifier. If none of these flags are specified, PlaySound searches the registry or the WIN.INI file for an association with the specified sound name. If an association is found, the sound event is played. If no association is found in the registry, the name is interpreted as a file name.

 

hmod
Handle to the executable file that contains the resource to be loaded. This parameter must be NULL unless SND_RESOURCE is specified in fdwSound.


fdwSound
Flags for playing the sound. The following table shows the possible values.
Value Description
SND_ALIAS The pszSound parameter is a system-event alias in the registry or the WIN.INI file. Do not use with either SND_FILENAME or SND_RESOURCE.
SND_ASYNC The sound is played asynchronously and PlaySound returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call PlaySound with pszSound set to NULL.
SND_FILENAME The pszSound parameter is a file name.
SND_LOOP The sound plays repeatedly until PlaySound is called again with the pszSound parameter set to NULL. You must also specify the SND_ASYNC flag to indicate an asynchronous sound event.
SND_MEMORY A sound event's file is loaded in RAM. The parameter specified by pszSound must point to an image of a sound in memory.
SND_NODEFAULT No default sound event is used. If the sound cannot be found, PlaySound returns silently without playing the default sound.
SND_NOSTOP The specified sound event will yield to another sound event that is already playing. If a sound cannot be played because the resource needed to generate that sound is busy playing another sound, the function immediately returns FALSE without playing the requested sound.
If this flag is not specified, PlaySound attempts to stop the currently playing sound so that the device can be used to play the new sound.
SND_NOWAIT If the driver is busy, return immediately without playing the sound.
SND_RESOURCE The pszSound parameter is a resource identifier; hmod must identify the instance that contains the resource. When creating the resource, you must identify it as being of the WAVE type, see Using PlaySound with a Resource Identifier.
SND_SYNC Synchronous playback of a sound event. PlaySound returns after the sound event completes.

 

 

Return Values
TRUE indicates success. FALSE indicates failure.

 

Remarks
Windows CE does not support the SND_ALIAS_ID and SND_PURGE flags for the fdwSound parameter. The sound specified by pszSound must fit into available physical memory and be playable by an installed waveform-audio device driver. PlaySound searches the file system directories for the sound according to the search order that was specified by the OEM of the target device. If it cannot find the specified sound, PlaySound uses the default system event sound entry instead. If the function can find neither the system default entry nor the default sound, it makes no sound and returns FALSE.

 

 

二、sndPlaySound函数用法

 

函数sndPlaySound的功能与PlaySound类似,但少了一个参数。

sndPlaySound函数的声明为:
BOOL sndPlaySound(LPCSTR lpszSound, UINT fuSound);

1、除了不能指定资源名字外,参数lpszSound与PlaySound的是一样的。

 

2、参数fuSound是如何播放声音的标志,可以是SND_ASYNC、SND_LOOP、SND_MEMORY、SND_NODEFAULT、SND_NOSTOP和SND_SYNC的组合,这些标志的含义与PlaySound的一样。

 

可以看出,sndPlaySound不能直接播放声音资源。要用该函数播放WAVE文件,可按下面的方式调用:
sndPlaySound(“MYSOUND.WAV”,SND_ASYNC); 

 

3、MSDN上关于sndPlaySound函数的用法如下:

This function plays a single waveform sound. To play multiple sounds simultaneously, use the waveOut* functions in the Waveform Audio API.

 

BOOL sndPlaySound(
  LPCTSTR lpszSoundName,
  UINT fuSound
);

 

Parameters


lpszSoundName
Long pointer to a null-terminated string that specifies the sound to play. This parameter can be either an entry in the registry or in WIN.INI that identifies a system sound, or it can be the name of a waveform-audio file. (If the function does not find the entry, the parameter is treated as a file name.) If this parameter is NULL, any currently playing sound is stopped.


fuSound
Flags for playing the sound. The following table shows the possible values.
Value Description
SND_ALIAS The pszSound parameter is a system-event alias in the registry or the WIN.INI file. Do not use with either SND_FILENAME or SND_RESOURCE.
SND_ASYNC The sound is played asynchronously and PlaySound returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call PlaySound with pszSound set to NULL.
SND_FILENAME The pszSound parameter is a file name.
SND_LOOP The sound plays repeatedly until PlaySound is called again with the pszSound parameter set to NULL. You must also specify the SND_ASYNC flag to indicate an asynchronous sound event.
SND_MEMORY A sound event's file is loaded in RAM. The parameter specified by pszSound must point to an image of a sound in memory.
SND_NODEFAULT No default sound event is used. If the sound cannot be found, PlaySound returns silently without playing the default sound.
SND_NOSTOP The specified sound event will yield to another sound event that is already playing. If a sound cannot be played because the resource needed to generate that sound is busy playing another sound, the function immediately returns FALSE without playing the requested sound.
If this flag is not specified, PlaySound attempts to stop the currently playing sound so that the device can be used to play the new sound.
SND_SYNC Synchronous playback of a sound event. PlaySound returns after the sound event completes.

 

Return Values
TRUE indicates success. FALSE indicates failure.

 

Remarks
The SND_NOSTOP flag controls how the current call to sndPlaySound behaves if a previous call is still playing.

If the specified sound cannot be found, sndPlaySound plays the system default sound. If there is no system default entry in the registry or WIN.INI file, or if the default sound cannot be found, the function makes no sound and returns FALSE.

The specified sound must fit in available physical memory and be playable by an installed waveform-audio device driver.

 

原创粉丝点击