Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 7-Nov-12 1:39am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
   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
  Permalink  
Comments
MR. AngelMendez at 13-Nov-12 4:41am
   
Thanks, sorry for the long reply. How do I embed the resource file?
Jason Gleim at 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 at 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)

  Print Answers RSS
0 OriginalGriff 359
1 Maciej Los 290
2 Sergey Alexandrovich Kryukov 230
3 Shweta N Mishra 185
4 PIEBALDconsult 149
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,760


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100