如何在C#中播放"New Mail"系统声音?这也称为"通知"声音.
在Win32中,这将是类似的
sndPlaySound('Notify', (SND_ALIAS or SND_ASYNC));
那么你如何在.Net中做到这一点?我知道你可以做到
System.Media.SystemSounds.Asterisk.Play();
但是那里有五种声音非常有限 - 不包括用户设置的新邮件声音.
当我收到新邮件并播放该文件时,我可以找出正在播放的.wav文件,但是当用户的声音方案发生变化时,这不会更新.
我最终做了什么:
我没有播放系统声音,而是将wav文件作为资源嵌入到应用程序中,然后播放它 System.Media.SoundPlayer
一种选择是直接将PInvoke直接导入sndSound API.这是该方法的PInvoke定义
public partial class NativeMethods { /// Return Type: BOOL->int ///pszSound: LPCWSTR->WCHAR* ///fuSound: UINT->unsigned int [System.Runtime.InteropServices.DllImportAttribute("winmm.dll", EntryPoint="sndPlaySoundW")] [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] public static extern bool sndPlaySoundW([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string pszSound, uint fuSound) ; /// SND_APPLICATION -> 0x0080 public const int SND_APPLICATION = 128; /// SND_ALIAS_START -> 0 public const int SND_ALIAS_START = 0; /// SND_RESOURCE -> 0x00040004L public const int SND_RESOURCE = 262148; /// SND_FILENAME -> 0x00020000L public const int SND_FILENAME = 131072; /// SND_ALIAS_ID -> 0x00110000L public const int SND_ALIAS_ID = 1114112; /// SND_NOWAIT -> 0x00002000L public const int SND_NOWAIT = 8192; /// SND_NOSTOP -> 0x0010 public const int SND_NOSTOP = 16; /// SND_MEMORY -> 0x0004 public const int SND_MEMORY = 4; /// SND_PURGE -> 0x0040 public const int SND_PURGE = 64; /// SND_ASYNC -> 0x0001 public const int SND_ASYNC = 1; /// SND_ALIAS -> 0x00010000L public const int SND_ALIAS = 65536; /// SND_SYNC -> 0x0000 public const int SND_SYNC = 0; /// SND_LOOP -> 0x0008 public const int SND_LOOP = 8; /// SND_NODEFAULT -> 0x0002 public const int SND_NODEFAULT = 2; }