C # How to play audio files on the console,

Source: Internet
Author: User

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

 

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.