不定系统扬声器蜂鸣声

发布于 2024-08-19 07:04:58 字数 101 浏览 7 评论 0原文

现在,我知道我可以使用 Console.Beep 播放设定持续时间的蜂鸣声,但我正在寻找的是一种无限期地播放特定频率声音的方法,例如开始和停止功能。有什么想法吗?

Now, I know that I can play a beep of a set duration with Console.Beep, but what I'm looking for is a way to indefinitely play a sound of a certain frequency, like a start and stop function. Any ideas?

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

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

发布评论

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

评论(3

云淡月浅 2024-08-26 07:04:58

如果您想发出控制台命名空间中提供的蜂鸣声以外的蜂鸣声,则必须直接与扬声器交谈。使用 Kernel32.dll 来执行此操作,如下所示:

首先确保包含互操作:

using System.Runtime.InteropServices;

然后为 Beep 添加 extern 方法,并从其他方法中使用它,这个方法将使扬声器以您想要的频率发出蜂鸣声。给它。

[DllImport("Kernel32.dll")]
public static extern bool Beep(Int32 frequency, Int32 duration);
public static void BeepFor(int mSec, int freq)
{
  int Frequency = freq;
  int DurationInMS = mSec;
  Beep(Frequency, DurationInMS);
}

If you want to make a beep other than the one offered in the Console namespace you have to talk directly to the speaker. Use the Kernel32.dll to do this like so:

First make sure to include interop:

using System.Runtime.InteropServices;

Then add the extern method for Beep, and use it from your other methods, this one will beep the speaker for as long as you want at the frequency you give it.

[DllImport("Kernel32.dll")]
public static extern bool Beep(Int32 frequency, Int32 duration);
public static void BeepFor(int mSec, int freq)
{
  int Frequency = freq;
  int DurationInMS = mSec;
  Beep(Frequency, DurationInMS);
}
生来就爱笑 2024-08-26 07:04:58

您也许可以使用MIDI

您还可以使用 PlaySound API 循环播放 WAV 文件指定 SND_LOOP 和 SND_ASYNC 的调用。

在现代 PC 上无限期地播放相同声音的概念有点变化无常,除非您处于可以编写和控制无限循环或只是以特定频率“打开”扬声器的硬件级别。更高级别的 API 和编程语言不会允许这样做,因为在该级别上,操作系统迫使您与其他应用程序良好地合作。

编辑

还应该注意的是,Beep 调用的持续时间以毫秒为单位,并且它确实采用 Int32 值。 Int32.MaxValue 毫秒是很长的时间。可能比任何人都更能忍受听到嘟嘟声。

You may be able to use MIDI.

You may also be able to loop a WAV file using the PlaySound API call with SND_LOOP and SND_ASYNC specified.

The concept of playing the same sound indefinitely on a modern PC is somewhat fickle unless you are at the the hardware level where you can write and control an infinite loop or just "turn on" the speaker at a certain frequency. The higher level API's and programming languages aren't going to allow this as, at that level, the OS is forcing you to play nice with other applications.

Edit

It should also be noted that the duration of the Beep call is in milliseconds and it does take an Int32 value. Int32.MaxValue milliseconds is looooong time. Probably more than anyone could stand listening to the beep.

猫烠⑼条掵仅有一顆心 2024-08-26 07:04:58

与这些问题一样,第一反应应该是:

你真正想要实现什么目标。您真的想要无限持续时间的蜂鸣声,还是想要播放更长的声音?

As always in these questions, the first response should be:

What are you really trying to accomplish. Do you really want a beep of indefinite duration, or do you want to play sounds for longer periods?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文