C # How to play audio files on the console,
OK. Check the code first:
1 using System. reflection; 2 using System. media; 3 4 namespace ThePlay 5 {6 class Program 7 {8 static void Main (string [] args) 9 {10 string namespaceName = Assembly. getExecutingAssembly (). getName (). name. toString (); 11 Assembly assembly = Assembly. getExecutingAssembly (); 12 SoundPlayer sp = new SoundPlayer (assembly. getManifestResourceStream (namespaceName + ". resources "+ ". STEAMBLO. WAV "); 13 Consol E. Write ("Hurry up, ship ~! "); 14 sp. Play (); 15 Console. ReadLine (); 16} 17} 18}
"Will you be very excited when you are on a ship for the first time ?"
"What?No, well, it's not normal for you to calm down"
Let's assume that you first saw the ship and first tried to ride the ship! Will you say to your friends, "Look, the ship is here ~!"
Yes, the above Code implements this scenario. When you say this, the ship is also quite well-formed, so there are just a few sounds!
Let's get down to the truth!
First, you must reference two class library files.
using System.Reflection;
using System.Media;
The code is simple.
String namespaceName = Assembly. GetExecutingAssembly (). GetName (). Name. ToString ();This code obtains the name of The namespace of the previous document, that is, the corresponding "ThePlay ".
Assembly assembly = Assembly. GetExecutingAssembly ();Gets the Assembly that contains the code currently executed.
SoundPlayer sp = new SoundPlayer (assembly. GetManifestResourceStream (namespaceName + ". Resources" + ". STEAMBLO. WAV "));
NamespaceName is the space name obtained above, Resource is the name of the audio folder, and STEAMBLO. WAV is the corresponding audio file! Case Sensitive! Otherwise, the result is null! Only. WAV audio files are supported!
What? Still cannot play?
That is normal, because there is another operation to complete the crash! The table is in a hurry. Please try again later ~ Haha!
OK. Step 1: Find the audio file you want to reference, right-click it, and the following menu appears. Then, locate the property and click it with the left-click button;
Step 2: Find the generation operation and change "content" to "embedded resources ".
Finally, press Ctrl + F5 to complete!
For the first technical blog, thank you!
For more highlights, add the group: 225443677