从 Windows 选择声音并播放它们

发布于 2024-10-20 01:31:42 字数 186 浏览 4 评论 0原文

我有一个 WinForms 应用程序。该应用程序有一个“首选项”部分,用户可以在其中选择显示警报时播放哪些声音。

是否可以有一个组合框,用户可以从 Windows 存储的声音中进行选择,例如“紧急停止”、“紧急蜂鸣”等。这些可以在“控制面板”>>中找到。 “声音和警报”部分。

是否也可以有一个播放按钮来测试声音?

I have a WinForms app. This app has a Preferences section where the user will be able to select which sounds are played when an alert is being displayed.

Is it possible to have a combobox where the user can select from the Windows stored sounds such as "critical stop", "critical beep" and so on. These are found in the "Control Panel" >> "Sounds and Alerts" section.

Is it also possible to have a play button to test the sounds out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

灯角 2024-10-27 01:31:42

您不需要任何 API 来播放系统声音,只需编写如下代码:

// Plays the sound associated with the Asterisk system event.
System.Media.SystemSounds.Asterisk.Play();

SystemSounds 类包含以下预定义的系统声音:

  • Asterisk
  • Beep
  • Exclamation
  • Hand
  • Question

所有其他声音都要求您从注册表中读取所需的声音并使用如下代码播放它:

SoundPlayer simpleSound = new SoundPlayer(@"c:\Path\To\Your\Wave\File.wav");

You do not require any API to play system sounds just write code like this:

// Plays the sound associated with the Asterisk system event.
System.Media.SystemSounds.Asterisk.Play();

The SystemSounds class contains the following predefined system sounds:

  • Asterisk
  • Beep
  • Exclamation
  • Hand
  • Question

All other sounds require you read the desired sound from the registry and play it with code like this:

SoundPlayer simpleSound = new SoundPlayer(@"c:\Path\To\Your\Wave\File.wav");
揽月 2024-10-27 01:31:42

试试这个:

    private void Form1_Load(object sender, EventArgs e)
    {

        var systemSounds = new[]
                              {
                                  System.Media.SystemSounds.Asterisk,
                                  System.Media.SystemSounds.Beep,
                                  System.Media.SystemSounds.Exclamation,
                                  System.Media.SystemSounds.Hand,
                                  System.Media.SystemSounds.Question
                              };

        comboBox1.DataSource = systemSounds;

        comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
    }

    void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ((System.Media.SystemSound)comboBox1.SelectedItem).Play();
    }

Try this:

    private void Form1_Load(object sender, EventArgs e)
    {

        var systemSounds = new[]
                              {
                                  System.Media.SystemSounds.Asterisk,
                                  System.Media.SystemSounds.Beep,
                                  System.Media.SystemSounds.Exclamation,
                                  System.Media.SystemSounds.Hand,
                                  System.Media.SystemSounds.Question
                              };

        comboBox1.DataSource = systemSounds;

        comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
    }

    void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ((System.Media.SystemSound)comboBox1.SelectedItem).Play();
    }
忆悲凉 2024-10-27 01:31:42

当然!您正在寻找的所有声音都可以通过System.Media.SystemSounds,它们在其中公开为 与触发声音的事件类型相对应的公共属性

此外,SystemSound 类< /a> 提供播放方法,您可以调用该方法来异步播放该声音。

例如,要播放“Critical Stop”声音,您只需编写以下代码:

System.Media.SystemSounds.Hand.Play();

Sure! All the sounds you're looking for are available through the System.Media.SystemSounds class, where they are exposed as public properties corresponding to the event types that trigger the sounds.

Additionally, objects of the SystemSound class provide a Play method that you can call to play that sound asynchronously.

So for example, to play the "Critical Stop" sound, you would simply write the following code:

System.Media.SystemSounds.Hand.Play();
小猫一只 2024-10-27 01:31:42

SystemSounds 仅涵盖几声声音。要玩其他游戏,您需要阅读注册表:

const string key = @"AppEvents\Schemes\Apps\.Default\Notification.Default\.Default";
using (var reg = Registry.CurrentUser.OpenSubKey(key))
using (var player = new SoundPlayer((string)reg.GetValue(string.Empty)))
{
    player.Play();
}

SystemSounds only cover a few sounds. For playing the others, you need to read the registry :

const string key = @"AppEvents\Schemes\Apps\.Default\Notification.Default\.Default";
using (var reg = Registry.CurrentUser.OpenSubKey(key))
using (var player = new SoundPlayer((string)reg.GetValue(string.Empty)))
{
    player.Play();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文