首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用参数指定播放参考资料中的哪个声音文件

使用参数指定播放参考资料中的哪个声音文件
EN

Stack Overflow用户
提问于 2016-10-05 22:40:14
回答 1查看 100关注 0票数 0

我在我的参考资料部分有大约20个小的音效文件...我想有一个方法,播放一个特定的声音文件的基础上,参数我传递它…假设我的声音文件名为sound01.wav、sound02.wav等。

代码语言:javascript
复制
public static void PlayMySound(string soundFile)
{
   SoundPlayer snd = new SoundPlayer(Properties.Resources.XXX);
   snd.Play()
}

PlayMySound(sound01);
PlayMySound(sound02);
etc.

在上面的代码中,我希望XXX是字符串soundFile

我在试着避免这样的事情...

代码语言:javascript
复制
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.
}
EN

回答 1

Stack Overflow用户

发布于 2016-10-05 22:52:00

好的,在我发布这篇文章后不久,答案就出现了……

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39877000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档