Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi!

I have a project that I am working on and in this project is a resource file called resource1 which I created through the GUI. I added music into the resource1 file but I am not sure how to play the music from there.

I've looked up some tutorials on how it's done which they use something like in this forum.

http://stackoverflow.com/questions/1900707/how-to-play-audio-from-resource[^]

they use something like:

properties.Resources."the name of file"

For some reason when I get to the Resources. part I am unable to find the music file's name to finish the code.

I've also tried this:

 SoundPlayer player = new SoundPlayer();
            player.SoundLocation.Equals(Resource1.Age_Of_Mythology_Soundtrack___Eat_Your_Potatoes);

player.Play();


Will appreciate all help,

Thanks.
Posted

1 solution

First thing is first... You don't set the player.SoundLocation property by calling the .Equal method. You would simply set it like player.SoundLocation = "location Uri"; The way you have that coded would return true or false whether the current sound location matched that string never actually setting the sound location.

However, you can't really do that because you embedded the sound in a resource file. You need to use the resource manager to load the resource from the .resx and then load it into the player. Something like this should do the trick:

C#
ResourceManager manager = new ResourceManager("Resource1",typeof(App).Assembly);
 Stream mediaStream = manager.GetStream("Age_Of_Mythology_Soundtrack___Eat_Your_Potatoes");
 SoundPlayer player = new SoundPlayer();
 player.Stream = mediaStream;
 player.Play();


Of course, the resource file has to be compiled as an embedded resource.

HTH
 
Share this answer
 
Comments
MR. AngelMendez 13-Nov-12 4:41am    
Thanks, sorry for the long reply. How do I embed the resource file?
Jason Gleim 13-Nov-12 14:58pm    
Add the file to the project and then open its properties. Change the file type to Embedded Resource.
MR. AngelMendez 15-Nov-12 19:25pm    
thanks it works :)

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