简体   繁体   中英

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

I am trying to play a sound with a key press. So far I have it working. However I want when I press the key again the sound to stop. I dont want to use an other key for soundplayer to stop. I want the same one.

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

            }
        }
    }

And here is the code from my main class which logs if the key is pressed

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

Define a boolean variable in your main class and add a StopSound void to your SCBA class.

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

This way you don't need to change your main 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));

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM