无法播放位于我的参考文件夹中的声音



我知道这很简单,但这让我发疯了。我已经将我的wave文件加载到我的应用程序资源/引用文件位置。我想做的就是播放这个波文件5秒钟。我在网上到处搜索,没有人有一个简单的解决方案。是我看得不够仔细,还是它比看起来更复杂?下面是我的代码示例:

if (attempts == win)
{
    label1.Text = "Great job!";
    level += 1;
    i = 0;
    attempts = "";
    win = "";
    cheaterLabel.Text = "";
}

我正在使用Visual Studio 2008。谢谢你!

示例来自这篇MSDN文章。

这比你想象的还要容易。

if (attempts == win)
{
playSimpleSound();
label1.Text = "Great job!";
level += 1;
i = 0;
attempts = "";
win = "";
cheaterLabel.Text = "";
}
 private void playSimpleSound()
{
SoundPlayer simpleSound = new SoundPlayer(@"c:SomeFilesomeSound.wav");
simpleSound.Play();
}

既然你说你有你的文件在你的应用程序资源,你应该能够做这样的事情使用SoundPlayer。我通过项目->属性->资源添加了这个文件。

SoundPlayer sp = new SoundPlayer(Properties.Resources.chord); \Change to what ever the name of your resource is
sp.Play();

最新更新