我希望能够一次播放多个声音。我使用多线程进行了尝试,但发现它们仍然可以一个接一个地播放。有办法让他们同时玩吗?

static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
            Thread th = new Thread(playSound);
            Thread th1 = new Thread(playSound1);

            th.Start();
            th1.Start();


        }

        public static void playSound()
        {
            System.Media.SoundPlayer s1 = new System.Media.SoundPlayer(@"c:\Users\Ben\Documents\c#\note_c.wav");
            s1.Load();
            s1.PlaySync();
        }

        public static void playSound1()
        {
            System.Media.SoundPlayer s1 = new System.Media.SoundPlayer(@"c:\Users\Ben\Documents\c#\note_e.wav");
            s1.Load();
            s1.PlaySync();
        }
    }

最佳答案

如果我们安排并行执行怎么样?
像那样:

var files = new List<string>() {"note_1.wav", "note_2.wav"};
Parallel.ForEach(files, (currentFile) =>
{
    System.Media.SoundPlayer s1 = new System.Media.SoundPlayer(currentFile);
    s1.Load();
    s1.PlaySync();
});

关于c# - 如何在C#中一次播放多种声音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51215385/

10-13 08:08