C# 如何在不每次创建新播放器的情况下播放声音

C# 如何在不每次创建新播放器的情况下播放声音,c#,wav,audio-player,soundplayer,C#,Wav,Audio Player,Soundplayer,我在一个表单项目中工作,在一些方法和事件中播放一些声音。 例如,在一个方法中,我有一个: SoundPlayer sndplayrONE = new SoundPlayer(Properties.Resources.wavsound1); sndplayrONE.Play(); 在另一个例子中,我有: SoundPlayer sndplayrTWO = new SoundPlayer(Properties.Resources.wavsound2); sndplayrTWO.Play();

我在一个表单项目中工作,在一些方法和事件中播放一些声音。 例如,在一个方法中,我有一个:

SoundPlayer sndplayrONE = new SoundPlayer(Properties.Resources.wavsound1);

sndplayrONE.Play();
在另一个例子中,我有:

SoundPlayer sndplayrTWO = new SoundPlayer(Properties.Resources.wavsound2);

sndplayrTWO.Play();
我想要的是在表单代码的末尾只创建soundplayer类的一个实例,比如

SoundPlayer sndplayr = new SoundPlayer(--some generic input---)
然后从任何事件中调用它,比如

sndplayr2.Play(sound1); sndplayr2.Play(sound2); sndplayr2.Play(sound3);
等等


我见过一些构造函数,比如
SoundPlayer(Stream)
SoundPlayer(String)
,但我一个都不懂。我需要一些短而简单的东西,就像我以前做的那样,但不需要每次我想播放声音时都创建一个新的实例。我在Resources.resx中使用了所有声音。

使用不同的构造函数


请查看此处的信息:

通过设置SoundLocation属性,然后执行Load()或LoadAsync(),似乎可以更改特定播放机将播放的声音

然而,制作一些类似于词典的东西,将一些您认为与当前上下文相关的SoundPlayer对象放入其中可能是明智的。比如:

Dictionary<string, SoundPlayer> sounds;

// Load sounds

// I wouldn't hardcode strings here, use constants or something. This is just an example.
sound["Sound1"].Play();
字典发音;
//加载声音
//我不会在这里硬编码字符串,使用常量之类的东西。这只是一个例子。
声音[“声音1”]。播放();

每次创建一个新实例对您来说真的有那么多工作吗?这不仅仅是工作,而且还可能要求效率和代码气味
Dictionary<string, SoundPlayer> sounds;

// Load sounds

// I wouldn't hardcode strings here, use constants or something. This is just an example.
sound["Sound1"].Play();