C#:发出连续的声音,直到被打断 [英] C#: Produce a continuous tone until interrupted

查看:59
本文介绍了C#:发出连续的声音,直到被打断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户可以与C#WPF程序进行交互的设备.当用户按下设备上的按钮达指定的时间或用户按下按钮的时间(以较短者为准)时,此程序必须发出蜂鸣声.唯一可以发出哔声/提示音的扬声器是计算机的BIOS扬声器.我们不能假设周围有其他说话者(并且可以肯定地说不会有其他说话者).

I have a device that a user interacts with alongside of a C# WPF program. This program must beep when the user presses a button on the device, for either a specified amount of time or for as long as the user presses the button, whichever is shorter. The only speaker available to produce the beep/tone is the computer BIOS speaker; we cannot assume that other speakers are around (and it's actually safe to assume that there won't be any other speakers).

如何在必要的时间内发出连续的声音?

How can I produce a continuous tone for the necessary duration?

到目前为止,我所发出的声音会发出很多蜂鸣声,但不会发出连续的声音.

What I have so far produces lots of beeps, but not a continuous tone.

首先,启动一个线程:

    private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) {
        if(this.Visibility == Visibility.Visible){
            mBeepThread = new Thread(new ThreadStart(ProduceTone));
            mBeepThread.Name = "Beep Thread";
            mBeepThread.Start();
        }
    }

线程本身:

    bool mMakeTone = false;
    private void ProduceTone(){
        while(this.Visibility == Visibility.Visible){
            if(mMakeTone ){
                Console.Beep();
            }
            else{
                Thread.Sleep(10);
            }
        }
    }

然后在按钮按下期间(直到设备本身指定的时间),将mMakeTone布尔值翻转为true.

And then the mMakeTone boolean is flipped to true during the duration of the button press, up to a time specified by the device itself.

我怀疑这只是对上面Console.Beep()行的快速更改,但我不确定会是什么.

I suspect that it's just a quick change to the Console.Beep() line above, but I'm not sure what it would be.

推荐答案

Console.Beep有一个过载,它需要一个频率和持续时间,如果您想在指定的持续时间内产生声音,这将非常有用.Console.Beep方法或我所知道的任何其他API都不直接支持先打开声音然后再使用BIOS扬声器将声音关闭的功能,而不必安装伪造的声卡驱动程序.

There is an overload for Console.Beep that takes a frequency and duration, which is useful if you want to produce a sound for a specified duration. The ability to turn on a sound and then later turn it off using the BIOS speaker is not directly supported by the Console.Beep method, or any other API that I am aware of, without perhaps installing a fake sound card driver.

一些实验发现了以下内容:

A little experimentation has discovered the following:

  • Console.Beep()是同步的,直到声音结束后才返回.
  • 从单独的线程调用Console.Beep()会中断当前正在进行的任何操作.

所以...您应该能够通过选择频率和很长的时间来创建一个新的后台线程来调用Console.Beep()来完成您的任务.要稍后终止声音,您可以在主线程上以任意频率和极长时间(例如非零)(例如1)调用Console.Beep()来终止从后台线程播放的声音.您不应使声音在后台线程上播放的时间超过合理的持续时间,因为该线程会一直存在,直到声音通常停止播放并且您不希望在后台堆积很多线程为止.

So... You should be able to accomplish your task by creating a new background thread to call Console.Beep() with the frequency of your choosing and a very long duration. To later terminate the sound, you may just call Console.Beep() on your main thread with any frequency and an extremely, non-zero duration (e.g. 1) to terminate the sound playing from the background thread. You should not make the sound playing on the background thread any longer that a reasonable duration because the thread will live until the sound would have ordinarily stopped playing and you don't want a lot of background threads piling up on you.

private void button1_Click(object sender, EventArgs e)
{
    // Starts beep on background thread
    Thread beepThread = new Thread(new ThreadStart(PlayBeep));
    beepThread.IsBackground = true;
    beepThread.Start();
}

private void button2_Click(object sender, EventArgs e)
{
    // Terminates beep from main thread
    Console.Beep(1000, 1);
}

private void PlayBeep()
{
    // Play 1000 Hz for 5 seconds
    Console.Beep(1000, 5000);
}

这篇关于C#:发出连续的声音,直到被打断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆