我在我的参考资料部分有大约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.
}发布于 2016-10-05 22:52:00
好的,在我发布这篇文章后不久,答案就出现了……
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);https://stackoverflow.com/questions/39877000
复制相似问题