【发布时间】: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();
}
【问题讨论】: