我有一个按钮,其中的文字显示音乐开/关。
使用以下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);
}
我的问题是,如果再次点击该按钮,如何指示该按钮关闭音乐,如果再按一下等等,则再将音乐重新打开。
答案 0 :(得分:6)
我不习惯写完整的代码来回答一个问题,但鉴于我现在感到多么无聊,并且考虑到播放的音乐,我无法抗拒制作小型状态机的冲动。
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方法播放,停止方法停止。理想情况下,您必须在完成歌曲后处理互斥锁。
答案 1 :(得分:0)
@Steve B在评论中的含义是正确的方法。您可能希望查看Timer Class,因为它很容易使用,并且可以帮助您实现他建议/您正在寻找的内容。
答案 2 :(得分:0)
请参阅此主题:How do I run a simple bit of code in a new thread?
您需要将音乐作为单独的线程运行到UI本身。