C# - 程序播放音频,任何一台电脑上?一台电脑、音频、程序

2023-09-03 07:46:55 作者:⌒尐掱栤涼だ

我想有一个复选框,可以启用/禁用背景音乐在我的WinForms应用程序。 我发现了如何从用打开文件对话框和这样的目录加载特定的文件,但是这是不好的,所以我这样做(在我的电脑工作):

I want to have a checkbox that can enable/disable background music in my winforms application. I found out how to load a specific file from a directory with openfiledialog and such but that was bad, so I did this (worked on my pc):

if (checkBox7.Checked == true)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.SoundLocation = "PATH";
            player.Load();
            player.Play();
        }

但是,如何在世界上我得到它的工作在其他计算机上? 我知道我可以把mp3文件相同的文件夹中的程序,并发送文件给其他人。但我见过那里的MP3 / WAV(或其他)是内置的应用程序中有只有一个exe文件的程序。

But how in the world do I get it to work on other computers? I know I could put an mp3 file inside the same folder with the program, and send that folder to another person. But I've seen programs where the mp3/wav (or whatever) is built-in inside the application and there's only an exe file.

一个例子是那些keygenerators针对不同的应用,如索尼拉斯维加斯。 他怎么有计划内的音频文件?

An example are those "keygenerators" for different applications like Sony Vegas. How did he include the audio file inside the program?

谁能帮忙? 我尝试添加一个wav文件的资源,然后使用它作为路径,但它不可能因为某些原因...?

Could anyone help? I tried adding a wav file to the resources and then use that as path, but it's not possible for some reason...?

路径将是例如: MyProgram.Properties.Resources.Song 但我不能在末尾加上.wav和MP3播放,所以它不能加载文件。

path would be for example: MyProgram.Properties.Resources.Song but I could not add .wav or .mp3 at the end, so it can't load the file.

任何帮助是AP preciated! 我想要的声音是在背景和隐藏,没有媒体播放器的表现等等。 而没有浏览文件按钮。我只是希望它自动加载我的歌应该包含以某种方式里面的程序。

Any help is appreciated! I want the sound to be in background and hidden, no media player showing and so on. And no "browse file" button. I just want it to load my song automatically which should be included inside the program in some way.

推荐答案

嵌入的资源(只是文件添加到您的项目中的资源,它会被嵌​​入到组装)。

Embed it as resource (just add that file to your project as resource and it'll be embedded inside assembly).

您将能够写(我假设你的资源文件被命名为Resources.resx和导入的资源音频文件NameOfYou​​rResource)本使用的声音播放接受流:

You'll be able to write (I assume your resource file is named Resources.resx and your imported resource for audio file is NameOfYourResource) this using SoundPlayer that accepts a stream:

using (var stream = Resources.ResourceManager.GetStream("NameOfYourResource"))
using (var player = new SoundPlayer(stream))
{
    player.PlaySync();
}

当然,这并不是唯一的方法!您可能(仍然使用资源)写入到一个临时文件(那么你会打的)。

Of course this is not the only method! You may (still using resources) write it to a temporary file (then you'll play that).

您也可以附加文件到你的端组件玩(是的,它已经被编译后):

You may also append files to play at the end of you assembly (yes, after it has been compiled):

在编译程序集。 作为生成后步骤添加音频文件到您的可执行文件(原始数据后,原始数据)。 添加额外的Int32块你组装它的大小没有音频文件。

要发挥它的:

阅读最后4个字节你自己的可执行文件。 打开一个流,并使用了大小偏移。 从那里播放。

这是更棘手,但它的优势是音频文件,不会像资源可见的(如果你在乎它,或者你想用这种方法别的东西)。

This is more tricky but it has advantage that audio file won't be visible as resource (in case you care about it or you want to use this method for something else).