【问题标题】:(C#) Play a sound when the key is pressed and stop it when the key is pressed again(C#) 按键时播放声音,再次按键时停止
【发布时间】:2020-04-15 03:30:57
【问题描述】:

我正在尝试通过按键播放声音。到目前为止,我有它的工作。但是我想当我再次按键时声音停止。我不想使用其他键来停止声音播放器。我想要一样的。

    public class SCBA
    {
         static string GameDirectory;
         static string soundDirectory;
         static SoundPlayer player;


        public static void InitializeSound()
        {
            player = new SoundPlayer();
            GameDirectory = Directory.GetCurrentDirectory();
            soundDirectory = GameDirectory + "/test/test/test/Audio";
            Game.LogTrivial("Sound Directory located at" + soundDirectory);

            try
            {
                player.SoundLocation = soundDirectory + "/test.wav";
            }
            catch(Exception e)
            {
                string error = e.Message;
                Game.LogTrivial("Sound File located at" + player.SoundLocation);
                Game.LogTrivial(String.Format("Something happened" + error));


            }

        }

        public static void PlaySound()
        {
            try
            {
                player.Play();
            }
            catch (Exception e)
            {
                Game.LogTrivial(e.ToString());
                Game.LogTrivial(String.Format("Something happened", e.Message));

            }
        }
    }

这是我的主类中的代码,它记录是否按下键

        if (Game.IsKeyDown(Settings.SCBA))
        {
            SCBA.PlaySound();
        }

【问题讨论】:

    标签: c# audio media


    【解决方案1】:

    这样你就不需要改变你的主类

    public class SCBA
    {
         static string GameDirectory;
         static string soundDirectory;
         static SoundPlayer player;
         private static bool isPlaying;
    
    
    
        public static void InitializeSound()
        {
            player = new SoundPlayer();
            GameDirectory = Directory.GetCurrentDirectory();
            soundDirectory = GameDirectory + "/test/test/test/Audio";
            Game.LogTrivial("Sound Directory located at" + soundDirectory);
            isPlaying = false;
    
            try
            {
                player.SoundLocation = soundDirectory + "/test.wav";
            }
            catch(Exception e)
            {
                string error = e.Message;
                Game.LogTrivial("Sound File located at" + player.SoundLocation);
                Game.LogTrivial(String.Format("Something happened" + error));
    
    
            }
    
        }
    
        public static void PlaySound()
        {
            try
            {
                if(isPlaying)
                {
                    player.Stop();
                }
                else
                {
                    player.Play();
                }
    
                isPlaying = !isPlaying;
            }
            catch (Exception e)
            {
                Game.LogTrivial(e.ToString());
                Game.LogTrivial(String.Format("Something happened", e.Message));
    
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      在您的主类中定义一个布尔变量并将StopSound void 添加到您的SCBA 类中。

      private bool needPlay = false;
      private void test()
      {
          if (Game.IsKeyDown(Settings.SCBA))
          {
              needPlay = !needPlay;
              if(needPlay)
                 SCBA.PlaySound();
              else
                 SCBA.StopSound();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多