【问题标题】:SoundPlayer not working when visual studio application is published发布 Visual Studio 应用程序时 SoundPlayer 不工作
【发布时间】:2020-11-24 21:10:59
【问题描述】:

当我在 Microsoft Visual Studio 中运行时,声音正常。但是,在我发布它之后,声音就不起作用了。声音属性设置为内容。有人可以帮忙吗?谢谢

private void InitializeSound()
        {
            // Create an instance of the SoundPlayer class.
            player = new SoundPlayer(@"Sounds\sirenpolice3.wav");
        }

enter image description here

【问题讨论】:

  • 发布时声音是否被复制到输出文件夹?
  • 输出文件夹是什么意思?发布后,只有一个应用程序(.exe)和一个程序调试数据库(.pdb)。应用发布时的部署模式是自包含的。
  • 您正在尝试从特定文件夹(相对于可执行文件)播放声音,是的,所有 DLL 等都是自包含的,资源不是。将您的声音文件夹复制到您正在运行的位置,它应该可以工作。如果您想嵌入声音,您需要将它们作为资源而不是文件夹。
  • 好的,所以我将音频的属性设置为资源。它抛出一个 FileNotFound 异常。我想我错过了一些步骤。我应该如何将其设置为资源?
  • 这应该会有所帮助。您需要将其创建为资源,然后将其作为资源访问。 stackoverflow.com/questions/90697/….

标签: c# visual-studio-code soundplayer


【解决方案1】:

试试这个示例

using (var player = new SoundPlayer(@"Sounds\sirenpolice3.wav"))
        {
            // Use PlaySync to load and then play the sound.
            // ... The program will pause until the sound is complete.
            player.PlaySync();
        }

【讨论】:

  • 没有。它仍然无法正常工作。声音在我发布后没有播放,但在 Visual Studio 中播放。
  • 这仍在尝试从发布后不存在的文件夹中读取它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-08
  • 1970-01-01
相关资源
最近更新 更多