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

使用PlaySound函数时需要在#include<windows.h>后面加上(注意:不能加在前面):

#include <mmsystem.h>
#pragma comment(lib, "WINMM.LIB")

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

参数hwnd是应用程序的实例句柄,除非pszSound的指向一个资源标识符(即fdwSound被定义为SND_RESOURCE),否则必须设置为NULL
 
参数fdwSound是标志的组合,如下表所示。若成功则函数返回TRUE,否则返回FALSE。
播放标志以及含义:
SND_APPLICATION 用应用程序指定的关联来播放声音。 // 默认播放方式
SND_ALIAS pszSound参数指定了注册表或WIN.INI中的系统事件的别名。                             //例如 SystemStart
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函数才返回。

播放方法

在C:\WINDOWS\MEDIA目录下有一个名为The Microsoft Sound.wav的声音文件,在Windows 95启动时会播放这个声音。下面我们用三种方法来调用PlaySound函数播出Windows 95的启动声音。
第一种方法是直接播出声音文件,相应的代码为:
PlaySound("c:\\WINDOWS\\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);

////////上面的(LPCTSTR)可能需改成(LPCWSTR)

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

PlaySound("SystemStart",NULL,SND_ALIAS|SND_ASYNC);
函数sndPlaySound的功能与PlaySound类似,但少了一个参数。函数的声明为:
BOOL sndPlaySound(LPCSTR lpszSound, UINT fuSound);
除了不能指定资源名字外,参数lpszSound与PlaySound的是一样的。参数fuSound是如何播放声 音的标志,可以是SND_ASYNC、SND_LOOP、SND_MEMORY、SND_NODEFAULT、SND_NOSTOP和SND_SYNC的 组合,这些标志的含义与PlaySound的一样。
可以看出,sndPlaySound不能直接播放声音资源。要用该函数播放WAVE文件,可按下面的方式调用:
sndPlaySound(“MYSOUND.WAV”,SND_ASYNC);
PlaySound(TEXT("C:\\windows\\Media\\Windows XP 关机.wav"),NULL,SND_SYNC);
要把资源添在Debug文件夹里

程序示例

#include "stdAfx.h"
#include <windows.h>
#include <mmsystem.h> // 加上,不然PlaySound函数无法使用
#pragma comment(lib, "WINMM.LIB") // 加上,不然PlaySound函数无法使用
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
    static TCHAR szAppName[] = TEXT ("HelloWin") ;
    HWND hwnd ;
    MSG msg ;
    WNDCLASS wndclass ;
    wndclass.style= CS_HREDRAW | CS_VREDRAW ;
    wndclass.lpfnWndProc = WndProc ;
    wndclass.cbClsExtra = 0 ;
    wndclass.cbWndExtra = 0 ;
    wndclass.hInstance = hInstance ;
    wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
    wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
    wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    wndclass.lpszMenuName = NULL ;
    wndclass.lpszClassName= szAppName ;
    if (!RegisterClass (&wndclass))
    {
        MessageBox ( NULL, TEXT ("This program requires Windows NT!"),szAppName, MB_ICONERROR) ;
        return 0 ;
    }
    hwnd = CreateWindow( szAppName, // window class name
    TEXT ("The Hello Program"), // window caption
    WS_OVERLAPPEDWINDOW, // window style
    CW_USEDEFAULT,// initial x position
    CW_USEDEFAULT,// initial y position
    CW_USEDEFAULT,// initial x size
    CW_USEDEFAULT,// initial y size
    NULL, // parent window handle
    NULL, // window menu handle
    hInstance, // program instance handle
    NULL) ; // creation parameters
    ShowWindow (hwnd, iCmdShow) ;
    UpdateWindow (hwnd) ;
    while (GetMessage (&msg, NULL, 0, 0))
    {
        TranslateMessage (&msg) ;
        DispatchMessage (&msg) ;
    }
    return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc ;
    PAINTSTRUCT ps ;
    RECT rect ;
    switch (message)
    {
    case WM_CREATE:
        PlaySound(TEXT("TESTWAVE.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
        return 0 ;
    case WM_PAINT:
        hdc = BeginPaint (hwnd, &ps) ;
        GetClientRect (hwnd, &rect) ;
        DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
        DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
        EndPaint (hwnd, &ps) ;
        return 0 ;
    case WM_DESTROY:
        PostQuitMessage (0) ;
        return 0 ;
    }
    return DefWindowProc (hwnd, message, wParam, lParam) ;
}

C#调用示例

using System;
using System.Runtime.InteropServices;
using System.Threading;
 
namespace MkSipSDK.Sound
{
    public class WavPlayer
    {
        protected const int SND_SYNC = 0x0;
        protected const int SND_ASYNC = 0x1;
        protected const int SND_NODEFAULT = 0x2;
        protected const int SND_MEMORY = 0x4;
        protected const int SND_LOOP = 0x8;
        protected const int SND_NOSTOP = 0x10;
        protected const int SND_NOWAIT = 0x2000;
        protected const int SND_ALIAS = 0x10000;
        protected const int SND_ALIAS_ID = 0x110000;
        protected const int SND_FILENAME = 0x20000;
        protected const int SND_RESOURCE = 0x40004;
        protected const int SND_PURGE = 0x40;
        protected const int SND_APPLICATION = 0x80;
 
 
        [DllImport("winmm")]
        public static extern bool PlaySound(string szSound, IntPtr hMod, int flags);
 
        /// <summary>
        /// 播发wav文件方法
        /// </summary>
        /// <param name="wavFile">wav文件路径</param>
        /// <param name="repeatCount">重复次数</param>
        private static void Play(string wavFile)
        {
            PlaySound(wavFile, IntPtr.Zero, SND_FILENAME | SND_SYNC); //同步,阻塞线程
            //PlaySound(wavFile, IntPtr.Zero, SND_FILENAME | SND_ASYNC); //异步,非阻塞线程
            //PlaySound(wavFile, IntPtr.Zero, 1 | 0x00020000 | repeatCount);
        }
 
        /// <summary>
        /// 响铃播放
        /// </summary>
        /// <param name="wavFile">wav文件路径</param>
        /// <param name="repeatCount">重复次数</param>
        public static void Ring(string wavFile, int repeatCount = 3)
        {
            ThreadPool.QueueUserWorkItem(delegate(object obj) {
                for (int i = 0; i < repeatCount; i++) {
                    Play(wavFile);
                }
            });
        }
    }
}