.net - Windows Form sound file does not exist and how to retrieve embedded sound (C++) -
the sound file located on project folder , added sound file resource files.
don't error when run debugger within visual studio 2012.
error when run application located in debug folder.
however, don't errors when include file location directory path.
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(); } } }; }
i able figure out.
found website:
https://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath%28v=vs.110%29.aspx
gave me path application is. moved .wav file path , added following statement:
player->soundlocation = string::concat(application::startuppath+"/soundbit.wav");
edit:
found better way , embed sound form1.resx ,
retrieve embedded sound.
had changed name of file "$this.soundbit" , add code:
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(); }
and namespaces:
using namespace system::componentmodel; // componentresourcemanager using namespace system::media; // soundplayer using namespace system::io; // memorystream