C++ Windows窗体声音文件不存在以及如何检索嵌入的声音(C+;+;)
声音文件位于我的项目文件夹中,我已将声音文件添加到我的资源文件中。C++ Windows窗体声音文件不存在以及如何检索嵌入的声音(C+;+;),c++,.net,visual-studio-2012,clr,C++,.net,Visual Studio 2012,Clr,声音文件位于我的项目文件夹中,我已将声音文件添加到我的资源文件中。在visual studio 2012中运行调试器时,我没有收到任何错误。 我在运行位于调试文件夹中的应用程序时出错 但是,当我包含文件位置目录路径时,我没有收到任何错误 namespace FORMv2 { //omitted code private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
在visual studio 2012中运行调试器时,我没有收到任何错误。
我在运行位于调试文件夹中的应用程序时出错 但是,当我包含文件位置目录路径时,我没有收到任何错误
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();
}
}
};
}
namespace FORMv2{
//省略代码
private:System::Void Form1_Load(System::Object^sender,System::EventArgs^e){
播放器=新的声音播放器;
//无误
//player->SoundLocation=“c//soundBit.wav”;
//错误
player->SoundLocation=“soundBit.wav”;
播放器->播放循环();
}
private:System::Void checkBoxEnable\u CheckedChanged(System::Object^sender,System::EventArgs^e){
如果(复选框启用->选中)
{
播放器->停止();
}
其他的
{
player->Play();
}
}
};
}
我能想出来我找到了这个网站:
这给了我应用程序所在的路径。我将.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();
}
当您运行exe时,您可能正在切换/加载一些其他文件,并且应用程序的路径已更改。所以您需要添加这个,System.Reflection.Assembly.getExecutionGassembly().Location+“soundBit.wav”;很抱歉评论晚了。我下班开车回家。不管怎么说,我添加了那个声明,我并没有得到任何错误,但并没有声音。这是我添加的语句(更改了语法)System::Reflection::Assembly::GetExecutionGassembly()->Location+“soundBit.wav”;
using namespace System::ComponentModel; // For ComponentResourceManager
using namespace System::Media; // For SoundPlayer
using namespace System::IO; // For MemoryStream