c++ - Windows 窗体声音文件不存在以及如何检索嵌入的声音 (C++)

标签 c++ .net visual-studio-2012 clr

声音文件位于我的项目文件夹中,我将声音文件添加到我的资源文件中。
当我在 visual studio 2012 中运行调试器时,我没有收到任何错误。
当我运行位于 Debug 文件夹中的应用程序时出现错误。

但是,当我包含文件位置目录路径时,我没有收到任何错误。

Sound file not located

namespace FORMv2 {

    //omitted code

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
             player = gcnew SoundPlayer;
             //no error
             //player->SoundLocation = "c/<path goes here>/soundBit.wav";
             // error
             player->SoundLocation = "soundBit.wav";
             player->PlayLooping();
         }    
private: System::Void checkBoxEnable_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
             if (checkBoxEnable->Checked)
             {
                 player->Stop();    
             }
             else
             {
                 player->Play();    
             }           
         }
    };
}

最佳答案

我想通了。
我找到了这个网站:
https://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath%28v=vs.110%29.aspx
这给了我应用程序所在的路径。我将 .wav 文件移动到该路径并添加了以下语句:

player->SoundLocation = String::Concat(Application::StartupPath+"/soundBit.wav");

编辑:
找到了一个更好的方法,那就是将声音嵌入到 Form1.resX 和
检索嵌入的声音。
我不得不将文件名更改为“$this.soundBit”并添加以下代码:

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
             ComponentResourceManager^  resources = (gcnew ComponentResourceManager(Form1::typeid));
             SoundPlayer^ player;
             Stream^ s = (cli::safe_cast<MemoryStream^ >(resources->GetObject(L"$this.soundBit")));
             player = gcnew SoundPlayer(s);
             player->Play();        
         }

和这个命名空间:

using namespace System::ComponentModel; // For ComponentResourceManager
using namespace System::Media; // For SoundPlayer
using namespace System::IO; // For MemoryStream

关于c++ - Windows 窗体声音文件不存在以及如何检索嵌入的声音 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29811134/

相关文章:

.net - 使用 Get-Member 进行反射

.net - 无法加载文件或程序集“System.Data.SQLite,版本=1.0.109.0 - 当我引用 1.0.109.1 时,为什么它会搜索版本 1.0.109.0

c# - C# 在同一个套接字上发送+接收数据

.net - 具有存储过程的数据集与 Entity Framework

javascript - 相同的代码,Fiddle 与 VS 中的不同结果

java - 在哪里可以找到 Java 和/或 C++ 的快速回顾?

c++ - 如何正确重写 ASSERT 代码以在 msvc 中传递/分析?

c++ - 通过引用传递智能指针

c++ - 加载的共享库中的类冲突

c# - 在单元测试项目中运行多个单元测试