c# - 打开/关闭音乐按钮 Console.Beep

标签 c# console beep

我有一个带有文本的按钮,上面写着音乐开/关。

使用以下 Console.Beep() 代码:

private void button1_Click(object sender, EventArgs e)
{
    Console.Beep(659, 125);
    Console.Beep(659, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(167);
    Console.Beep(523, 125);
    Console.Beep(659, 125);
    Thread.Sleep(125);
    Console.Beep(784, 125);
    Thread.Sleep(375);
    Console.Beep(392, 125);
    Thread.Sleep(375);
    Console.Beep(523, 125);
    Thread.Sleep(250);
    Console.Beep(392, 125);
    Thread.Sleep(250);
    Console.Beep(330, 125);
    Thread.Sleep(250);
    Console.Beep(440, 125);
    Thread.Sleep(125);
    Console.Beep(494, 125);
    Thread.Sleep(125);
    Console.Beep(466, 125);
    Thread.Sleep(42);
    Console.Beep(440, 125);
    Thread.Sleep(125);
    Console.Beep(392, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(125);
    Console.Beep(784, 125);
    Thread.Sleep(125);
    Console.Beep(880, 125);
    Thread.Sleep(125);
    Console.Beep(698, 125);
    Console.Beep(784, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(125);
    Console.Beep(523, 125);
    Thread.Sleep(125);
    Console.Beep(587, 125);
    Console.Beep(494, 125);
    Thread.Sleep(125);
    Console.Beep(523, 125);
    Thread.Sleep(250);
    Console.Beep(392, 125);
    Thread.Sleep(250);
    Console.Beep(330, 125);
    Thread.Sleep(250);
    Console.Beep(440, 125);
    Thread.Sleep(125);
    Console.Beep(494, 125);
    Thread.Sleep(125);
    Console.Beep(466, 125);
    Thread.Sleep(42);
    Console.Beep(440, 125);
    Thread.Sleep(125);
    Console.Beep(392, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(125);
    Console.Beep(784, 125);
    Thread.Sleep(125);
    Console.Beep(880, 125);
    Thread.Sleep(125);
    Console.Beep(698, 125);
    Console.Beep(784, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(125);
    Console.Beep(523, 125);
    Thread.Sleep(125);
    Console.Beep(587, 125);
    Console.Beep(494, 125);
    Thread.Sleep(375);
    Console.Beep(784, 125);
    Console.Beep(740, 125);
    Console.Beep(698, 125);
    Thread.Sleep(42);
    Console.Beep(622, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(167);
    Console.Beep(415, 125);
    Console.Beep(440, 125);
    Console.Beep(523, 125);
    Thread.Sleep(125);
    Console.Beep(440, 125);
    Console.Beep(523, 125);
    Console.Beep(587, 125);
    Thread.Sleep(250);
    Console.Beep(784, 125);
    Console.Beep(740, 125);
    Console.Beep(698, 125);
    Thread.Sleep(42);
    Console.Beep(622, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(167);
    Console.Beep(698, 125);
    Thread.Sleep(125);
    Console.Beep(698, 125);
    Console.Beep(698, 125);
    Thread.Sleep(625);
    Console.Beep(784, 125);
    Console.Beep(740, 125);
    Console.Beep(698, 125);
    Thread.Sleep(42);
    Console.Beep(622, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(167);
    Console.Beep(415, 125);
    Console.Beep(440, 125);
    Console.Beep(523, 125);
    Thread.Sleep(125);
    Console.Beep(440, 125);
    Console.Beep(523, 125);
    Console.Beep(587, 125);
    Thread.Sleep(250);
    Console.Beep(622, 125);
    Thread.Sleep(250);
    Console.Beep(587, 125);
    Thread.Sleep(250);
    Console.Beep(523, 125);
    Thread.Sleep(1125);
    Console.Beep(784, 125);
    Console.Beep(740, 125);
    Console.Beep(698, 125);
    Thread.Sleep(42);
    Console.Beep(622, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(167);
    Console.Beep(415, 125);
    Console.Beep(440, 125);
    Console.Beep(523, 125);
    Thread.Sleep(125);
    Console.Beep(440, 125);
    Console.Beep(523, 125);
    Console.Beep(587, 125);
    Thread.Sleep(250);
    Console.Beep(784, 125);
    Console.Beep(740, 125);
    Console.Beep(698, 125);
    Thread.Sleep(42);
    Console.Beep(622, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(167);
    Console.Beep(698, 125);
    Thread.Sleep(125);
    Console.Beep(698, 125);
    Console.Beep(698, 125);
    Thread.Sleep(625);
    Console.Beep(784, 125);
    Console.Beep(740, 125);
    Console.Beep(698, 125);
    Thread.Sleep(42);
    Console.Beep(622, 125);
    Thread.Sleep(125);
    Console.Beep(659, 125);
    Thread.Sleep(167);
    Console.Beep(415, 125);
    Console.Beep(440, 125);
    Console.Beep(523, 125);
    Thread.Sleep(125);
    Console.Beep(440, 125);
    Console.Beep(523, 125);
    Console.Beep(587, 125);
    Thread.Sleep(250);
    Console.Beep(622, 125);
    Thread.Sleep(250);
    Console.Beep(587, 125);
    Thread.Sleep(250);
    Console.Beep(523, 125);
    Thread.Sleep(625);
}

我的问题是如何让按钮在再次单击时关闭音乐,然后在再次单击时重新打开音乐,依此类推。

最佳答案

我不习惯编写完整的代码来回答问题,但考虑到我现在有多无聊,考虑到播放的音乐,我无法抑制制作一个小型状态机的冲动。

public class Player
{
    private Thread Thread { get; set; }

    private ManualResetEventSlim Mutex { get; set; }

    public Player()
    {
        this.Mutex = new ManualResetEventSlim(false);

        this.Thread = new Thread(this.PlayThread);
        this.Thread.Start();
    }

    public void Play()
    {
        this.Mutex.Set();
    }

    public void Stop()
    {
        this.Mutex.Reset();
    }

    private static IEnumerable<Action> PlaySong()
    {
        yield return () => Console.Beep(659, 125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(167);
        yield return () => Console.Beep(523, 125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(784, 125);
        yield return () => Thread.Sleep(375);
        yield return () => Console.Beep(392, 125);
        yield return () => Thread.Sleep(375);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(392, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(330, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(440, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(494, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(466, 125);
        yield return () => Thread.Sleep(42);
        yield return () => Console.Beep(440, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(392, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(784, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(880, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(698, 125);
        yield return () => Console.Beep(784, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(587, 125);
        yield return () => Console.Beep(494, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(392, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(330, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(440, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(494, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(466, 125);
        yield return () => Thread.Sleep(42);
        yield return () => Console.Beep(440, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(392, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(784, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(880, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(698, 125);
        yield return () => Console.Beep(784, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(587, 125);
        yield return () => Console.Beep(494, 125);
        yield return () => Thread.Sleep(375);
        yield return () => Console.Beep(784, 125);
        yield return () => Console.Beep(740, 125);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(42);
        yield return () => Console.Beep(622, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(167);
        yield return () => Console.Beep(415, 125);
        yield return () => Console.Beep(440, 125);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(440, 125);
        yield return () => Console.Beep(523, 125);
        yield return () => Console.Beep(587, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(784, 125);
        yield return () => Console.Beep(740, 125);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(42);
        yield return () => Console.Beep(622, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(167);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(698, 125);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(625);
        yield return () => Console.Beep(784, 125);
        yield return () => Console.Beep(740, 125);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(42);
        yield return () => Console.Beep(622, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(167);
        yield return () => Console.Beep(415, 125);
        yield return () => Console.Beep(440, 125);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(440, 125);
        yield return () => Console.Beep(523, 125);
        yield return () => Console.Beep(587, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(622, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(587, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(1125);
        yield return () => Console.Beep(784, 125);
        yield return () => Console.Beep(740, 125);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(42);
        yield return () => Console.Beep(622, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(167);
        yield return () => Console.Beep(415, 125);
        yield return () => Console.Beep(440, 125);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(440, 125);
        yield return () => Console.Beep(523, 125);
        yield return () => Console.Beep(587, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(784, 125);
        yield return () => Console.Beep(740, 125);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(42);
        yield return () => Console.Beep(622, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(167);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(698, 125);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(625);
        yield return () => Console.Beep(784, 125);
        yield return () => Console.Beep(740, 125);
        yield return () => Console.Beep(698, 125);
        yield return () => Thread.Sleep(42);
        yield return () => Console.Beep(622, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(659, 125);
        yield return () => Thread.Sleep(167);
        yield return () => Console.Beep(415, 125);
        yield return () => Console.Beep(440, 125);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(125);
        yield return () => Console.Beep(440, 125);
        yield return () => Console.Beep(523, 125);
        yield return () => Console.Beep(587, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(622, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(587, 125);
        yield return () => Thread.Sleep(250);
        yield return () => Console.Beep(523, 125);
        yield return () => Thread.Sleep(625);
    }

    private void PlayThread()
    {
        foreach (var action in PlaySong())
        {
            if (!this.Mutex.IsSet)
            {
                this.Mutex.Wait();
            }

            action();
        }
    }
}

用法:实例化 Player 类。使用 Play 方法播放,使用 Stop 方法停止。理想情况下,您必须在完成歌曲后处理互斥体。

关于c# - 打开/关闭音乐按钮 Console.Beep,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12217054/

相关文章:

node.js - azure 函数 - 在 azure 门户上运行 npm install

Java:完全禁用任何 Swing 不需要的蜂鸣声的方法?

c# - 在 C# 中解决全局热键处理的最佳方法?

swift - Xcode 8 (Swift) 控制台中没有打印输出

c# - 我可以在 .NET 应用程序中临时覆盖 DNS 解析吗?

javascript - js控制台和rails控制台打印相同时间不同正常吗?

logging - Ubuntu 终端中的音频反馈

iphone - iPhone等手机上发出哔哔声:使用javascript

c# - (C#) TcpClient 没有连接到 irc 服务器(使用数字 ip)

c# - wp - 根据 slider 值将图像转换为灰度