(C#) 按下按键时播放声音,再次按下按键时停止

(C#) Play a sound when the key is pressed and stop it when the key is pressed again

我正在尝试通过按键播放声音。到目前为止,我已经开始工作了。但是我希望当我再次按下该键时声音停止。我不想使用其他键让声音播放器停止。我要一样的

    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));

            }
        }
    }

这里是我的主要 class 中的代码,它会在按键被按下时记录下来

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

在主 class 中定义一个布尔变量,并在 SCBA class.

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

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

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));

        }
    }
}