Click here to Skip to main content
15,993,835 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using Visual Studio 2010 for C++
I need to play a soundeffect for my C++ program with SoundPlayer. The sound effect is called Help.wav. It is a RESOURCE FILE. The file is in a folder called 'sounds' within the resource files.

I have already typed:
C++
using namespace System::Media;
SoundPlayer^ player = gcnew SoundPlayer();
player->SoundLocation =;
player->Load();
player->Play();


I am having trouble finding out what to type for the "player->SoundLocation =;" Line.
Any help would be appreciated.
Posted
Updated 27-Jul-12 8:00am
v3
Comments
Sergey Alexandrovich Kryukov 27-Jul-12 13:58pm    
This is not C++, this is C++/CLI, a different language (and not the "managed extension" which was superseded). So, first tag it properly.
--SA

1 solution

Please see:
http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx[^],
http://msdn.microsoft.com/en-us/library/system.media.soundplayer.soundlocation.aspx[^].

First article contains more advanced code sample you need to understand to write anything with this class.

[EDIT]

Sorry, I did not pay attention for your complication: this is a resource. (And please disregard my comment where I blamed you for missing information; I apologize.) Resource is not exactly a file, but you can use the resource as stream. In this case, you don't need to use location at all. You need to use a different constructor:
http://msdn.microsoft.com/en-us/library/ms143770.aspx[^].

For a constructor parameter, you can use a resource stream. Please see this discussion on how to obtain a resource stream:
http://stackoverflow.com/questions/1388052/resource-from-assembly-as-a-stream[^].

—SA
 
Share this answer
 
v5
Comments
iluvatar003 27-Jul-12 14:16pm    
The documentation tells you how to open a file from a file path. I am not doing that. I am using a file that is embedded into the project. And I read the manual. That isn't funny.
Sergey Alexandrovich Kryukov 27-Jul-12 14:18pm    
I already apologized for not understanding your question and updated the answer, please see.
--SA
Sergey Alexandrovich Kryukov 27-Jul-12 14:20pm    
Nevertheless, the documentation does say all you need, not just about the file path, so you still need to read it properly.
Now, if the solution clear now? If not, your follow-up questions are welcome.
--SA
iluvatar003 27-Jul-12 14:33pm    
The second link you gave is for C# not C++. Would it be possible for you to write the code I'm missing? I have this:
using namespace System::Media;
SoundPlayer^ player = gcnew SoundPlayer();
SoundPlayer(Stream^ s);
player->Load();
player->Play();
I don't know what to type to pass the wav file into the stream and the documentation does not provide examples.
Sergey Alexandrovich Kryukov 27-Jul-12 15:32pm    
Is a C# link a problem for you. All .NET language have a big equivalent part...
Isn't that obvious:
SoundPlayer^ player = gcnew SoundPlayer(myResourseStream);
...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900