模拟录音和播放的过程

        控制台有一个强大的类Console,它包含了很多的方法,使控制台看起来不是那么的死板。其中Beep()方法就可以实现音频播放的功能。
        一下是实现发音的一个方法,传入的参数为一个字符型的,因为要获取的是用户按下键盘上的哪个键。
        static void Play(char ch)
        {
            switch (ch)
            {
                case '1':
                    Console.BackgroundColor = ConsoleColor.Blue;     //改变其背景的颜色
                    Console.Clear();
                    i = 1;
                    Console.Beep((i + 13) * 37, 200);                                   //Beep()方法的使用
                    break;
                case '2':
                    Console.BackgroundColor = ConsoleColor.Cyan;
                    Console.Clear();
                    i = 3;
                    Console.Beep((i + 13) * 37, 200);
                    break;
                case '3':
                    Console.BackgroundColor = ConsoleColor.DarkBlue;
                    Console.Clear();
                    i = 5;
                    Console.Beep((i + 13) * 37, 200);
                    break;
                case '4':
                    Console.BackgroundColor = ConsoleColor.DarkCyan;
                    Console.Clear();
                    i = 6;
                    Console.Beep((i + 13) * 37, 200);
                    break;
                case '5':
                    Console.BackgroundColor = ConsoleColor.DarkGray;
                    Console.Clear();
                    i = 8;
                    Console.Beep((i + 13) * 37, 200);
                    break;
                case '6':
                    Console.BackgroundColor = ConsoleColor.DarkGreen;
                    Console.Clear();
                    i = 10;
                    Console.Beep((i + 13) * 37, 200);
                    break;
                case '7':
                    Console.BackgroundColor = ConsoleColor.DarkMagenta;
                    Console.Clear();
                    i = 12;
                    Console.Beep((i + 13) * 37, 200);
                    break;
                case '8':
                    Console.BackgroundColor = ConsoleColor.Gray;
                    Console.Clear();
                    i = 15;
                    Console.Beep((i + 13) * 37, 200);
                    break;
                default:
                    Console.BackgroundColor = ConsoleColor.DarkYellow;
                    Console.Clear();
                    i = 1;
                    Console.Beep((i + 13) * 37, 200);
                    break;
            }
        }
 
          怎样获取用户在键盘上输入的键,录音结束标记是哪个,这都是需要考虑的。
            while (true)
            {
                Console.WriteLine("a.开始录音 b.播放录音");
                char choose = Convert.ToChar(Console.ReadLine());
                switch (choose)
                {
                    case 'a':
                        ConsoleKeyInfo key1 = new ConsoleKeyInfo();
                        while (key1.KeyChar != 's')                         //当用户输入s时,表明输入结束
                        {
                            key1 = Console.ReadKey(true);                     //获取用户输入的键值
                            a = key1.KeyChar;
                            list.Add(a);
                            Console.Write(a);
                            Play(a);
                        }
                        break;
                    case 'b':
                        if (list.Count > 0)
                        {
                            foreach (char ch in list)
                            {
                                Play(ch);
                            }
                        }
                        else
                        {
                            Console.WriteLine("你还没有录音,请先录音!");
                        }
                        break;
                }
 
Console类中的好多方法,属性都值得我们去深入的研究!

你可能感兴趣的:(职场,休闲,学习日志4)