我的参考资料部分有大约20个小的声音效果文件...我想有一个方法根据我传递的参数播放特定的声音文件...假设我的声音文件被命名sound01.wav,sound02.wav等。
public static void PlayMySound(string soundFile)
{
SoundPlayer snd = new SoundPlayer(Properties.Resources.XXX);
snd.Play()
}
PlayMySound(sound01);
PlayMySound(sound02);
etc.
在上面的代码中,我希望XXX成为字符串soundFile
我试图避免这样的事情......
public static void PlayMySound(string soundFile)
{
if (soundFile == "sound01") { SoundPlayer snd = new SoundPlayer(Properties.Resources.sound01); }
if (soundFile == "sound02") { SoundPlayer snd = new SoundPlayer(Properties.Resources.sound02); }
if (soundFile == "sound03") { SoundPlayer snd = new SoundPlayer(Properties.Resources.sound03); }
if (soundFile == "sound04") { SoundPlayer snd = new SoundPlayer(Properties.Resources.sound04); }
if (soundFile == "sound05") { SoundPlayer snd = new SoundPlayer(Properties.Resources.sound05); }
etc.etc.etc.
}
答案 0 :(得分:0)
好的,我在发布此消息后不久就回答了我......
public static void PlayMySound(Stream soundFile)
{
SoundPlayer snd = new SoundPlayer(soundFile);
snd.Play();
}
// Then I can just call it like this...
PlayMySound(Properties.Resources.sound01);
PlayMySound(Properties.Resources.sound02);