首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打开/关闭音乐按钮Console.Beep

打开/关闭音乐按钮Console.Beep
EN

Stack Overflow用户
提问于 2012-08-31 22:07:50
回答 3查看 6.2K关注 0票数 4

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

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

代码语言:javascript
复制
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);
}

我的问题是,如果它再次被点击,我如何告诉按钮关闭音乐,如果它再次被点击,我如何让它重新打开音乐,以此类推。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-31 22:40:50

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

代码语言:javascript
复制
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方法停止。理想情况下,您必须在完成歌曲后处理互斥量。

票数 6
EN

Stack Overflow用户

发布于 2012-08-31 22:18:43

@Steve B在评论中的内容是正确的方法。你可能想看看Timer Class,因为它很容易使用,并将帮助你实现他所建议的/你正在寻找的东西。

票数 0
EN

Stack Overflow用户

发布于 2012-08-31 22:25:16

请查看此帖子:How do I run a simple bit of code in a new thread?

您需要将音乐作为UI本身的单独线程运行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12217054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档