How the C # console plays audio files

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.