OK, first look at the code:
1 usingSystem.Reflection;2 usingSystem.Media;3 4 namespaceTheplay5 {6 class Program7 {8 Static voidMain (string[] args)9 {TenString namespacename =assembly.getexecutingassembly (). GetName (). Name.tostring (); OneAssembly Assembly =assembly.getexecutingassembly (); ASoundPlayer SP =NewSoundPlayer (assembly. GetManifestResourceStream (NamespaceName +". Resources"+". Steamblo. WAV")); -Console.Write ("Look, the ship is here! "); - sp. Play (); the console.readline (); - } - } -}
"Will you be very excited when you first take the ship?" ”
"What?" No, okay, you're a little bit out of the ordinary.
Suppose a scene, the first time you see a ship, first try to ride a ship! You will not shout to the pot friend said: "Look, the ship arrived ~!" ”
Yes, the above code to achieve this is the scene, when you said this sentence, the ship is quite occasional, came a few whistle sound!
All right, let's go!
The first thing to do is to refer to two class library files
using System.Reflection;
using System.Media;
The code is simple.
string namespacename = assembly.getexecutingassembly (). GetName (). Name.tostring (); This code is to obtain the name of the previous document namespace, which is the corresponding "Theplay".
Assembly Assembly = assembly.getexecutingassembly (); Gets the assembly that contains the code that is currently executing.
New " . Resources"". Steamblo. WAV")); NamespaceName is the space name obtained above, resource is the name of the audio folder, Steamblo. WAV is the corresponding audio file! Case Sensitive! Otherwise it will result in the acquisition being empty!
What the? Or is it not playing?
That is normal phenomenon, because there is a operation of the wood has finished! Table anxious, please listen to my slowly way ~ haha ha!
OK, the first step: Find the audio file you want to reference, right-click, the following menu appears, and then find the properties, left-click;
Step two: Find the Build Action and change "content" to "Embedded Resource".
Finally, Ctrl+f5, it's done!
First technical Blog, welcome everyone to advise, thank you!
For more information, please Dabigatran: 225443677
C # console How to play audio files