C# 播放和暂停播放wav文件
[DllImport("winmm.DLL",
EntryPoint = "PlaySound",
SetLastError = true,
CharSet = CharSet.Unicode,
ThrowOnUnmappableChar = true)]
private static extern bool PlaySound(string szSound, System.IntPtr hMod,
PlaySoundFlags flags);
[System.Flags]
public enum PlaySoundFlags : int
{
SND_SYNC = 0x0000, //同步播放声音,在播放完后PlaySound函数才返回
SND_ASYNC = 0x0001, //用异步方式播放声音,PlaySound函数在开始播放后立即返回
SND_NODEFAULT = 0x0002, //不播放缺省声音,若无此标志,则PlaySound在没找到声音时会播放缺省声音
SND_LOOP = 0x0008, //重复播放声音,必须与SND_ASYNC标志一块使用
SND_NOSTOP = 0x0010, //PlaySound不打断原来的声音播出并立即返回FALSE
SND_NOWAIT = 0x00002000, //如果驱动程序正忙则函数就不播放声音并立即返回
SND_FILENAME = 0x00020000, //pszSound参数指定了WAVE文件名
SND_RESOURCE = 0x00040004 //pszSound参数是WAVE资源的标识符,这时要用到hmod参数
}
public void runSound()
{
PlaySound(@"E:\报警声音\ALARM1.WAV", IntPtr.Zero,
SND_ASYNC | SND_FileNAME | SND_LOOP);
}
private void button1_Click(object sender, EventArgs e)
{
PlaySound(null, IntPtr.Zero, SND_ASYNC);
}
private void button2_Click(object sender, EventArgs e)
{
new Thread(new ThreadStart(runSound)).Start();
}