c# - .Net中如何播放 "New Mail"系统音?

标签 c# .net audio

如何在C#中播放“新邮件”的系统声音?这也称为“通知”声音。

在 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;
}

关于c# - .Net中如何播放 "New Mail"系统音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/695241/

相关文章:

c# - 如何将 DataRow[] 转换为 List<string[]>

c# - 存储来自 C# 应用程序的数据

java - 在Java中选择声卡以播放声音

c# - 64 位 PC 上的 int.MaxValue 是什么?

audio - 为什么我不能用 ffmpeg 重新排序我的 mpg 容器中的流?

linux - ALSA:snd_pcm_writei 返回 EAGAIN

c# - 检查基本拉丁字母 (A-Z) 的最简单方法

c# - 如何在 WPF 中使用多个 ViewModel 并通过一个 MainViewModel 绑定(bind)它们?

c# - 使用 AutoMapper 和 DI 容器实例化类型

.net - 在 SQL Server 2008 中使用 SqlBulkCopy 需要什么权限?