【问题标题】:make a continuous beep sound until interrupted in c#在c#中发出连续的哔声直到被打断
【发布时间】:2015-09-04 11:53:01
【问题描述】:

我想在我的应用程序中发出持续的哔声直到被中断。

接近

  1. 显示带有重试取消按钮的消息框
  2. 持续发出哔哔声,直到我按下取消键
  3. 如果按下重试,哔声不应停止


我使用 Console.beep() 尝试过这个,但它只发出一次声音。
有什么想法吗??

【问题讨论】:

  • 您的用户会喜欢这个功能的。
  • 你能发布一些代码吗?
  • 这应该会有所帮助:stackoverflow.com/questions/1195828/…
  • 我试过这个,但它只在点击重试时播放哔声
    public void retry() { if (MessageBox.Show("Item cannot be found", "Alert", MessageBoxButtons.RetryCancel) == DialogResult.Retry) { Console.Beep(5000, 250); retry(); } }
  • 在您的问题中发布您的代码,而不是在评论部分!

标签: c# audio beep


【解决方案1】:

你当然知道,这个方法有更多的构造函数……你可以指定它是frequencyduration

Console.Beep(100,10000);

对您的情况的另一个建议是根据布尔变量循环它:

   bool stop = false;

        while (!stop)
        {
            Console.Beep();
        }

【讨论】:

  • @Jamiec 我试过这个,但第二个参数是时间限制,我的上级对此并不满意。他想连续播放。
  • @GajendraRajput 你的意思是永远玩下去?无限?
  • 有点,但是当点击取消时应该停止。
  • @GajendraRajput 我明白了。请编辑查看我的更新答案。
  • @GajendraRajput - 有点困惑为什么(以及如何!!)你告诉我这个
【解决方案2】:

Console.Beep(); 有两个参数,长度和音调。

像这样使用:Console.Beep(tone_in_hz, length_in_milliseconds);

您还可以创建一个新线程和一个公共布尔值,然后运行一个 while 循环,如下所示:

private void Beeper()
{
    while(makeBeepSound)
    {
        Console.Beep();
    }
}

【讨论】:

    【解决方案3】:

    试试这个

       public void Beep()
        {
            if (MessageBox.Show("Message", "Alert", MessageBoxButtons.RetryCancel) == DialogResult.Retry)
            {
                    Console.Beep(5000, 5000);
                    Beep();
            }
        }
    

    在 Form1 加载事件中

        private void Form1_Load(object sender, EventArgs e)
        {
            Beep();
        }
    

    【讨论】:

      【解决方案4】:

      将 Beep() 放在一个循环中并在一个新的踏板中启动它。例如:

          class Beeper
          {
              private int _gapMiliseconds;
              private bool _stop = false;
      
              public Beeper(int gapMiliseconds)
              {
                  _gapMiliseconds = gapMiliseconds;
              }
      
              public void Start()
              {
                  while (!_stop)
                  {
                      Console.Beep();
                      Thread.Sleep(_gapMiliseconds);
                  }
              }
      
              public void Stop()
              {
                  _stop = true;
              }
          }
      

      以及将其作为线程启动和停止的代码。

              Beeper beeper = new Beeper(100);
              Thread thread = new Thread(new ThreadStart(beeper.Start));
              thread.Start();
              Thread.Sleep(5000);
              beeper.Stop();
      

      【讨论】:

      • 您提供的解决方案没有帮助
      猜你喜欢
      • 2010-11-14
      • 1970-01-01
      • 2016-07-18
      • 2012-04-21
      • 1970-01-01
      • 2011-05-02
      • 2010-09-24
      • 1970-01-01
      相关资源
      最近更新 更多