Playing sounds on Console - C#

后端 未结 3 1780
我在风中等你
我在风中等你 2021-01-03 11:09

I\'m writing a Console application on C# and I want to play a sound when I display texts continuously. This is what I\'ve done :

static SoundPlayer typewrite         


        
3条回答
  •  余生分开走
    2021-01-03 11:24

    Here's something that might help you out (please note that this code is for a winforms app, but you should be able to convert to a console app. Just study the code to see how it works) You'll basically be adding the .wav file as a 'resource' to your program. Then, your program can access the .wav file and play it:

    enter image description here

    using System.Reflection;
    using System.IO;
    using System.Resources;
    using System.Media;
    using System.Diagnostics;
    
    
    
    namespace Yournamespace
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void Form2_Load(object sender, EventArgs e)
            {
                Assembly assembly;
                Stream soundStream;
                SoundPlayer sp;
                assembly = Assembly.GetExecutingAssembly();
                sp = new SoundPlayer(assembly.GetManifestResourceStream
                    ("Yournamespace.Dreamer.wav"));
                sp.Play();  
            } 
        }
    }
    

提交回复
热议问题