有没有办法发布使用System.Media Soundplayer在.NET Framework上运行的C#控制台应用?

发布于 2025-02-10 15:58:56 字数 746 浏览 0 评论 0 原文

当我发布我的应用程序并运行它时,它说它找不到通往 sound.wav 文件的路径,并且崩溃了。

我将声音文件放入 bin/debug/sound 文件夹中,然后在我的代码中使用它:

public void OpenInventory()
{
    SoundPlayer inventory = new SoundPlayer("sound/selection2.wav");

    inventory.Play();

    try
    {
        var item = Item.itemList.SingleOrDefault(x => x.Amount == 0);

        if (item != null)
        {
            Item.itemList.Remove(item);
        }

        var loot = Loot.lootList.SingleOrDefault(c => c.Amount == 0);

        if (loot != null)
        {
            Loot.lootList.Remove(loot);
        }
     }
     catch
     {
         inter.InventoryArray();
         UseItem();
     }

     inter.InventoryArray();
     UseItem();
}

When I publish my app and run it, it says that it couldn't find the path to sound.wav file and it crashes.

I put my sound files into a bin/Debug/sound folder, and use it like this in my code:

public void OpenInventory()
{
    SoundPlayer inventory = new SoundPlayer("sound/selection2.wav");

    inventory.Play();

    try
    {
        var item = Item.itemList.SingleOrDefault(x => x.Amount == 0);

        if (item != null)
        {
            Item.itemList.Remove(item);
        }

        var loot = Loot.lootList.SingleOrDefault(c => c.Amount == 0);

        if (loot != null)
        {
            Loot.lootList.Remove(loot);
        }
     }
     catch
     {
         inter.InventoryArray();
         UseItem();
     }

     inter.InventoryArray();
     UseItem();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝情姑娘 2025-02-17 15:58:56

您可以将文件嵌入为资源,并使用资源流访问它。类似的内容:

strong resource name = "MyNamespace.Sound.selection2.wav";
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
var inventory = new SoundPlayer(stream);

要嵌入WAV文件,请将“构建操作”更改为解决方案资源管理器中的文件属性上的“嵌入式资源”。

使用此类嵌入式资源的棘手部分是找出正确的资源名称。默认情况下,它为资源使用类似命名空间的名称。因此,例如,如果您的应用程序的默认命名空间为“ mynamespace”,则该文件位于名为“ Sound”的文件夹中,并且该文件命名为“ Selection2.Wav”,则资源将被命名为“ myNamespace.sound.selection2” .wav“。

getManifestResourceStream https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.getmanifestresourcestream?view=netframework-4.8#system- Reflection-eSembly-getManifestResourceStream(System-string)

soundplayer 流构造器: https://learn.microsoft.com/en-comrosoft.com/en-us/dotnet /PI/system.Media.soundplayer.-ctor?view=netFramework-4.8#system-media-soundplayer-ctor-ctor-ctor(System-io-stream)

嵌入式资源名称: https://learn.microsoft.com/en-us/en-us/dotnet/core/core/resources/manifest-manifest-ector-文件名

You can embed the file as a resource and access it using a resource stream. Something like this:

strong resource name = "MyNamespace.Sound.selection2.wav";
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
var inventory = new SoundPlayer(stream);

To embed the WAV file, change "Build Action" to "Embedded Resource" on the file properties in solution explorer.

The tricky part of using such embedded resources is figuring out the correct resource name. By default it uses a namespace-like name for the resource. So for example, if the default namespace for your app is "MyNamespace", the file is in a folder named "Sound", and the file is named "selection2.wav", then the resource will be named "MyNamespace.Sound.selection2.wav".

GetManifestResourceStream: https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.getmanifestresourcestream?view=netframework-4.8#system-reflection-assembly-getmanifestresourcestream(system-string)

SoundPlayer stream constructor: https://learn.microsoft.com/en-us/dotnet/api/system.media.soundplayer.-ctor?view=netframework-4.8#system-media-soundplayer-ctor(system-io-stream)

Embedded resource names: https://learn.microsoft.com/en-us/dotnet/core/resources/manifest-file-names

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文